Java simple solution


  • 0
    C

    Reference: CTCI

    public TreeNode sortedArrayToBST(int[] nums) {
            return sortedArrayToBST(nums,0,nums.length-1); 
            
        }
          private TreeNode sortedArrayToBST(int[] nums, int start, int end) {
            if(end<start)
                return null;
            int mid=(start+end)/2;
            TreeNode node=new TreeNode(nums[mid]);
            node.left=sortedArrayToBST(nums,start,mid-1);
            node.right=sortedArrayToBST(nums,mid+1,end);
            return node;
        }
    
    

Log in to reply
 

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