-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortedArrToBST.java
36 lines (34 loc) · 1.18 KB
/
sortedArrToBST.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Q. - Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
Used the logic of Inorder trasversal. As the inorder traversal also gives us a sorted manner values, the middle or the mean value of the value range is generally the root node of the tree.
Using this, I have built the entire BST
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode createBST(int nums[], int l, int r){
if (l > r) {
return null;
}
int rootData = l+(r-l)/2;
TreeNode root = new TreeNode(nums[rootData]);
root.left = createBST(nums,l,rootData-1);
root.right = createBST(nums,rootData+1,r);
return root;
}
public TreeNode sortedArrayToBST(int[] nums) {
int n = nums.length;
return createBST(nums, 0, n - 1);
}
}