Skip to content

Commit c962921

Browse files
authored
Merge pull request #1068 from NAVJOT-786/main
added leetcode solution for problem 108
2 parents 98e541e + ca2530a commit c962921

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
id: convert-sorted-array-to-binary-search-tree
3+
title: Convert Sorted Array to Binary Search Tree
4+
sidebar_label: 0108-convert-sorted-array-to-binary-search-tree
5+
tags:
6+
- Array
7+
- Binary search tree
8+
- Divide and Conquer
9+
- Tree
10+
- Binary Tree
11+
description: "Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree."
12+
---
13+
14+
15+
### Problem Description
16+
17+
Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
18+
19+
### Examples
20+
21+
#### Example 1
22+
23+
- **Input:** ` nums = [-10,-3,0,5,9]`
24+
- **Output:** `[0,-3,9,-10,null,5]`
25+
26+
#### Example 2
27+
28+
- **Input:** ` nums = [1,3]`
29+
- **Output:** `[3,1]`
30+
31+
### Constraints
32+
33+
- $1 \leq \text{nums.length} \leq 10^4$
34+
- $-10^4 \leq \text{nums}[i] \leq 10^4$
35+
- nums is sorted in a strictly increasing order
36+
37+
### Solution Code
38+
39+
#### Python
40+
41+
```python
42+
class Solution:
43+
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
44+
total_nums = len(nums)
45+
if not total_nums:
46+
return None
47+
48+
mid_node = total_nums // 2
49+
return TreeNode(
50+
nums[mid_node],
51+
self.sortedArrayToBST(nums[:mid_node]), self.sortedArrayToBST(nums[mid_node + 1 :])
52+
)
53+
```
54+
55+
#### Java
56+
57+
```java
58+
class Solution {
59+
public TreeNode sortedArrayToBST(int[] nums) {
60+
int n=nums.length;
61+
if (n ==0) return null;
62+
if (n ==1) {
63+
TreeNode head = new TreeNode(nums[0]);
64+
return head;
65+
}
66+
67+
TreeNode head = buildBST(nums,0,n-1);
68+
return head;
69+
}
70+
private TreeNode buildBST (int [] nums, int low, int high){
71+
if(low>high) return null;
72+
73+
int mid =low+(high-low)/2;
74+
TreeNode node = new TreeNode(nums[mid]);
75+
node.left =buildBST(nums,low,mid-1);
76+
node.right =buildBST(nums,mid+1,high);
77+
return node;
78+
}
79+
}
80+
```
81+
82+
#### C++
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
TreeNode* sortedArrayToBST(vector<int>& nums) {
88+
return constructBSTRecursive(nums, 0, nums.size() - 1);
89+
}
90+
91+
TreeNode* constructBSTRecursive(vector<int>& nums, int left, int right) {
92+
if(left > right)
93+
return NULL;
94+
int mid = left + (right - left) / 2;
95+
TreeNode* node = new TreeNode(nums[mid]);
96+
node->left = constructBSTRecursive(nums, left, mid - 1);
97+
node->right = constructBSTRecursive(nums, mid + 1, right);
98+
return node;
99+
}
100+
};
101+
```
102+
#### Javascript
103+
104+
```javascript
105+
var sortedArrayToBST = function(nums) {
106+
if(!nums.length) return null;
107+
let mid = Math.floor(nums.length / 2);
108+
let root = new TreeNode(nums[mid]);
109+
root.left = sortedArrayToBST(nums.slice(0, mid));
110+
root.right = sortedArrayToBST(nums.slice(mid + 1));
111+
return root;
112+
};
113+
```

0 commit comments

Comments
 (0)