Posted on 2022-11-13 20:05
Uriel 阅读(47)
评论(0) 编辑 收藏 引用 所属分类:
搜索 、
数据结构 、
闲来无事重切Leet Code
深度优先后序遍历一棵树
1 #590
2 #Runtime: 102 ms
3 #Memory Usage: 16.4 MB
4
5 """
6 # Definition for a Node.
7 class Node(object):
8 def __init__(self, val=None, children=None):
9 self.val = val
10 self.children = children
11 """
12
13 class Solution(object):
14 def DFS(self, r, ans):
15 if not r:
16 return None
17 for i in r.children:
18 self.DFS(i, ans)
19 ans.append(r.val)
20 return ans
21
22 def postorder(self, root):
23 """
24 :type root: Node
25 :rtype: List[int]
26 """
27 return self.DFS(root, [])