Simple JavaScript Solution


  • 0
    W
    var constructMaximumBinaryTree = function(nums) {
        
        let insert = function (tree, node) {
            if (tree && tree.val > node.val) {
                let temp = insert(tree.right, node)
                tree.right = temp
                return tree
            } else {
                node.left = tree
                return node
            }
        }
        
        let res = null
        
        for (let num of nums) {
            let node = new TreeNode(num)
            res = insert(res, node)
        }
        
        return res
    }
    

Log in to reply
 

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