题目链接: https://leetcode-cn.com/problems/binary-tree-postorder-traversal/.
后序遍历原则:
先遍历左子树,再遍历右子树,最后遍历根节点。
递归版本:
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if not root:
return []
if not root.left and not root.right:
return [root.val]
return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val]