java single recursive solution


  • 0
    Y

    public TreeNode sortedArrayToBST(int[] nums) {
    if (nums.length == 0)
    return null;
    int index = nums.length / 2;
    int[] left = Arrays.copyOfRange(nums, 0, index);
    int[] right = Arrays.copyOfRange(nums, index + 1, nums.length);
    int val = nums[index];
    TreeNode root = new TreeNode(val);
    root.left = sortedArrayToBST(left);
    root.right = sortedArrayToBST(right);
    return root;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.