Javascript two stacks without recursion


  • 0
    B
     var postorderTraversal = function(root) {
        var stack = [];
        var values = [];
        
        if (root)
            stack.push(root);
        
        while(stack.length > 0) {
            var node = stack.pop();
            
            values.unshift(node.val);
            
            if (node.left)
                stack.push(node.left);
                
            if (node.right)
                stack.push(node.right);
        }
        
        return values;
    };

Log in to reply
 

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