108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return helper(nums,0,nums.length-1);
}
public TreeNode helper(int[] nums,int start,int end) {
if(start>end)
return null;
int len=end-start+1;
TreeNode root = new TreeNode(nums[start+len/2]);
root.left=helper(nums,start,start+len/2-1);
root.right=helper(nums,start+len/2+1,end);
return root;
}
}