108.Convert Sorted Array to Binary Search Tree

Solution 1: accepted 2ms

A very readable solution with extra spaces used.

1
2
3
4
5
6
7
8
public TreeNode sortedArrayToBST(int[] nums) {
if (nums.length < 1) return null;
int mid = nums.length / 2;
TreeNode root = new TreeNode(nums[mid]);
root.left = sortedArrayToBST(Arrays.copyOfRange(nums, 0, mid));
root.right = sortedArrayToBST(Arrays.copyOfRange(nums, mid + 1, nums.length));
return root;
}