-
Notifications
You must be signed in to change notification settings - Fork 5
/
0145.py
47 lines (45 loc) · 1.36 KB
/
0145.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def postorderTraversal(self, root: TreeNode) -> List[int]:
# 递归
res = []
def dfs(root):
if not root:
return []
dfs(root.left)
dfs(root.right)
res.append(root.val)
dfs(root)
return res
# 迭代
res = []
if not root:
return res
stack = []
node = root
while stack or node:
while node:
# 根节点入栈
stack.append(node)
# 左子树存在
if node.left:
node = node.left
# 左子树不存在,转右子树
else:
node = node.right
# 取出栈顶元素
node = stack.pop()
res.append(node.val)
# 栈不为空且当前节点是栈顶元素的左节点
# stack[-1]就是取出的栈顶元素:node
if stack and node == stack[-1].left:
node = stack[-1].right
# 没有左子树或右子树,退栈
else:
node = None
return res