Simple Iterative Python


  • 0
    E
        def postorderTraversal(self, root):
            stack, res = [], []
            prev = None
            while stack or root:
                if root:
                    stack.append(root)
                    root = root.left
                elif stack[-1].right and stack[-1].right != prev:
                    root = stack[-1].right
                else: # either right child has been processed or no right child
                    prev = stack.pop()
                    res.append(prev.val)
            return res
    

Log in to reply
 

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