None traversal javascript code


  • 0
    J
    var invertTree = function(root) {
        if(root === null)return root;
        var stack = [root]
        while(stack.length !== 0){
            var top = stack[stack.length-1];
            var temp  = top.left;
            top.left  = top.right;
            top.right = temp; 
            stack.pop()
            if(top.left !== null) {
                stack.push(top.left);
            }
            if(top.right !== null) {
                stack.push(top.right);
            }
        }
        return root;
    };

Log in to reply
 

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