Javascript 9-line solution


  • 0
    C
    var sortedArrayToBST = function(nums) {
        if (nums.length===0) return null;
        return build(0, nums.length);
        function build(start, length){
            let leftL = Math.floor((length)/2), head = new TreeNode(nums[start+leftL]);
            if (leftL>0) head.left = build(start, leftL);
            if (leftL+1<length) head.right = build(start+leftL+1, length-leftL-1);
            return head;
        }
    };

Log in to reply
 

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