Javascript solution


  • 0
    R
    var sortedArrayToBST = function(nums) {
        if(nums.length === 0) return nums;
            
        var half = Math.floor(nums.length / 2),
            root = new TreeNode(nums[half]);
            
        if(nums.slice(0, half).length > 0)
            root.left = sortedArrayToBST(nums.slice(0, half));
        if(nums.slice(half + 1).length > 0)
            root.right = sortedArrayToBST(nums.slice(half + 1));
            
        return root;
    };

Log in to reply
 

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