Sorted Array to BST - Java, 1ms


  • 0
    V
        private TreeNode constructBST(int[] a, int s, int e) {
            if( s==e ) return new TreeNode(a[s]);
            if ( s > e ) return null;
            int mid = (s+e)/2 ;
            TreeNode current = new TreeNode(a[mid]);
            current.left = constructBST(a, s, mid-1);
            current.right = constructBST(a, mid+1, e);
            return current;
        }
        public TreeNode sortedArrayToBST(int[] nums) {
            return constructBST(nums, 0, nums.length-1);
        }
    

Log in to reply
 

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