JavaScript iterative DFS solution


  • 1
    L
    function invertTree(root) {
        var stack = [root];
        while (stack.length) {
            var n = stack.pop();
            if (!n) {
                continue;
            }
            [n.left, n.right] = [n.right, n.left];
            stack.push(n.left, n.right);
        }
        return root;
    }

Log in to reply
 

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