Question
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
Code
public TreeNode build(int[] nums, int left, int right) {
if (left <= right) {
int mid = (left + right) / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = build(nums, left, mid - 1);
root.right = build(nums, mid + 1, right);
return root;
}
return null;
}
public TreeNode sortedArrayToBST(int[] nums) {
return build(nums, 0, nums.length - 1);
}