Java with helper recursion


  • 0
    Y
    public TreeNode sortedArrayToBST(int[] nums) {
            return treefy(nums, 0, nums.length-1);
        }
        public TreeNode treefy(int[] nums, int l, int r)
        { 
            if(l>r) return null;
            int m =l+(r-l)/2;
            TreeNode t = new TreeNode(nums[m]);
            t.left = treefy(nums, l, m-1);
            t.right = treefy(nums, m+1, r);
            return t;
        }
    

Log in to reply
 

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