-
Notifications
You must be signed in to change notification settings - Fork 13
/
N叉树的前序遍历.py
52 lines (47 loc) · 1.13 KB
/
N叉树的前序遍历.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
48
49
50
51
52
# 给定一个
# N
# 叉树,返回其节点值的前序遍历。
#
# 例如,给定一个
# 3
# 叉树:
#
# 返回其前序遍历: [1, 3, 5, 6, 2, 4]。
#
#
#
# 说明: 递归法很简单,你可以使用迭代法完成此题吗?
# 方法一:递归
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def preorder(self, root: 'Node') -> List[int]:
if not root:
return []
a = []
for i in root.children:
a += self.preorder(i)
return [root.val] + a
# 方法二:迭代
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution:
def preorder(self, root: 'Node') -> List[int]:
if root is None:
return []
stack, output = [root, ], []
while stack:
root = stack.pop()
output.append(root.val)
stack.extend(root.children[::-1])
return output