Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
深度优先后序遍历一棵树

 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, [])

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理