forked from dimpeshpanwar/javabasicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convert Sorted Array to Binary Search Tree
39 lines (35 loc) · 1.57 KB
/
Convert Sorted Array to Binary Search Tree
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
37
38
39
//Question : Given an integer array nums where the elements are sorted in ascending order, convert it to a
//height-balanced binary search tree.
/**
* 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 sortedArrayToBST(int[] nums) {
return CreateBST(nums, 0, nums.length - 1);
}
private TreeNode CreateBST(int nums[], int l, int r) {
if (l > r) { // Base Condition or Recursion Stoping Condition
return null;
}
// so basically in this question we have to convert sorted array to height balanced tree
// so if we directly create tree in given sorted order it will become linked list
// so we have to take middle element as head value such it will become height balanced tree
int mid = l + (r - l) / 2; // this is the formula to find mid value
TreeNode root = new TreeNode(nums[mid]); // mid value or median
root.left = CreateBST(nums, l, mid - 1); // assign the value for left of subtree that is l to mid -1 for given array
root.right = CreateBST(nums, mid + 1, r); // assign the value for right go subtree that is mid+1 to r for given array
return root;
}
}