My java solution


  • 0
    J
    public TreeNode sortedArrayToBST(int[] nums) {
        if (nums.length < 1) return null;
        TreeNode tree = new TreeNode(nums[(nums.length)/2]);
        createTree(0,nums.length,tree,nums);
        return tree;
    }
    void createTree(int left,int right,TreeNode tree,int[] nums){
        int mid = (left + right) / 2;
        if ( left < mid) {
            tree.left = new TreeNode(nums[(left+mid)/2]);
            createTree(left,mid,tree.left,nums);
        }
        if ( mid + 1< right) {
            tree.right = new TreeNode(nums[(mid+1+right)/2]);
            createTree(mid+1,right,tree.right,nums);
        }
    }

Log in to reply
 

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