Recursive JAVA Solution implemented using Binary Search Concept


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

Log in to reply
 

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