-
Notifications
You must be signed in to change notification settings - Fork 5
/
0590.py
36 lines (32 loc) · 814 Bytes
/
0590.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
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def postorder(self, root: 'Node') -> List[int]:
# DFS
res = []
def dfs(root):
if not root:
return
for child in root.children:
dfs(child)
res.append(root.val)
dfs(root)
return res
# BFS
if not root:
return []
q = [root]
res = []
while q:
# 弹出列表尾部的一个元素
node = q.pop()
res.append(node.val)
# 顺序加入
for child in node.children:
q.append(child)
return res[::-1]