8.10 - hard - 32

145. Binary Tree Postorder Traversal

这种题一看就是不会做,哈哈哈,不会做还挺有理的。多写几遍,手熟就好。这种反转preorder的想法还是挺奇妙的。

class Solution(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        result = []
        stack = []
        p = root
        while stack or p:
            if p:
                stack.append(p)
                result = [p.val] + result # Reverse the process of preorder
                p = p.right              # Reverse the process of preorder
            else:
                node = stack.pop()
                p = node.left          # Reverse the process of preorder
        return result

你可能感兴趣的:(8.10 - hard - 32)