145. 二叉树的后序遍历



方法一:迭代

class Solution(object):
    # 迭代
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        ans = []
        post_stack = []
        cur = root
        while post_stack or cur:
            while cur:
                ans.append(cur.val)
                post_stack.append(cur)
                cur = cur.right
            temp = post_stack.pop()
            cur = temp.left
        return ans[::-1]

方法二:递归

class Solution(object):
	# 递归
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val]
原文地址:https://www.cnblogs.com/panweiwei/p/13585761.html