[JavaScript] Simple JavaScript solution with syntactic sugar


  • 0
    D
    var addOneRow = function(root, v, d, dir = 'left') {
        if (d === 1) {
            const node = new TreeNode(v);
            [root, node[dir]] = [node, root]
        } else if (root) {
            root.left = addOneRow(root.left, v, d - 1, 'left')
            root.right = addOneRow(root.right, v, d - 1, 'right')
        }
        return root
    };
    

Log in to reply
 

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