JavaScript solution easy to understand.


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

Log in to reply
 

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