Simple Readable Python Iterative Solution


  • 0
    D
    class Solution(object):
        def postorderTraversal(self, root):
            if not root:
                return []
            ret = []
            stack = [(root, 'visit')]
            while stack:
                node, action = stack.pop()
                if action == 'visit':
                    stack.append((node, 'append'))
                    if node.right:
                        stack.append((node.right, 'visit'))
                    if node.left:
                        stack.append((node.left, 'visit'))
                else:
                    # action == 'append'
                    ret.append(node.val)
            return ret

Log in to reply
 

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