Just reverse the result of preorder, but visit right subtree before left tree


  • 0
    Y
    class Solution:
            # @param root, a tree node
            # @return a list of integers
            def postorderTraversal(self, root):
                node=root
                stack=[]
                res=[]
                while stack or node:
                    if node:
                        res.append(node.val)
                        stack.append(node)
                        node=node.right
                    else:
                        node=stack.pop()
                        node=node.left
                res.reverse()
                return res

Log in to reply
 

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