Simple Iterative Python

  • 0
        def postorderTraversal(self, root):
            stack, res = [], []
            prev = None
            while stack or root:
                if 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()
            return res

Log in to reply

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