Intuitive Javascript Solution


  • 0
    D
    var sortedArrayToBST = function(nums) {
        if (!nums.length) return null;
        
        const mid = Math.floor(nums.length / 2);
        const root = new TreeNode(nums[mid]);
        
        // subtrees are BSTs as well
        root.left = sortedArrayToBST(nums.slice(0, mid));
        root.right = sortedArrayToBST(nums.slice(mid + 1));
        
        return root;
    };
    

Log in to reply
 

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