Javascript 12 lines accepted solution


  • 0
    B
    var sortedArrayToBST = function(nums) {
        if (nums === null || nums.length === 0) {
            return null;
        }
        
        var mi = Math.floor(nums.length / 2);
        var node = new TreeNode(nums[mi]);
        
        if (mi > 0) {
            node.left = sortedArrayToBST(nums.slice(0, mi));
            node.right = sortedArrayToBST(nums.slice(mi + 1, nums.length));
        }
        
        return node;
    }

Log in to reply
 

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