Treat as a reverse preorder Traversal


  • 1
    L
    class Solution(object):
        def postorderTraversal(self, root):
            ret = []
            stack = [root]
            while stack:
                node = stack.pop()
                if node:
                    ret.append(node.val)
                    stack.append(node.left)
                    stack.append(node.right)
            return ret[::-1]

  • 0
    D

    Actually it's not preorder. Preorder is : root->left->right, but here is : root->right->left. However it's a great idea!


Log in to reply
 

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