My Python solution using stack, no reverse.


  • 0
    G
        def postorderTraversal(self, root):
            if not root:
                return []
            stack = [root]
            pos = []
            while stack:
                top = stack[-1]
                if top.left:
                    stack.append(top.left)
                    top.left = None
                    continue
                if top.right:
                    stack.append(top.right)
                    top.right = None
                    continue
                pos.append(top.val)
                stack.pop()
            return pos

Log in to reply
 

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