# 145. Binary Tree Postorder Traversal 二叉树的后序遍历

## 刷题内容

• https://leetcode.com/problems/binary-tree-postorder-traversal

给定一个二叉树，返回它的 后序 遍历。

1
\
2
/
3



## 解题方案

class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
res = []
if not root:
return res

left = self.postorderTraversal(root.left)
right = self.postorderTraversal(root.right)

if left:
res.extend(left)
if right:
res.extend(right)

res.append(root.val)
return res


class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
def postOrder(root):
if not root : return
postOrder(root.left)
postOrder(root.right)
res.append(root.val)

res = []
postOrder(root)
return res


class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
res = []
if not root:
return res

stack1 = []
stack1.append(root)

while len(stack1) > 0:
node = stack1.pop()
res.append(node.val)
if node.left:
stack1.append(node.left)
if node.right:
stack1.append(node.right)

return res[::-1]