-
Notifications
You must be signed in to change notification settings - Fork 95
/
遍历-层次遍历.py
54 lines (43 loc) · 1.17 KB
/
遍历-层次遍历.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
53
54
"""
一、题目
按层次的遍历的方法打印一颗树
例如有如下一棵树:
5
/ \
3 6
/ \ \
2 4 7
打印结果是
5 3 6 2 4 7
二、思路
层次遍历的步骤是:
对于不为空的结点,先把该结点加入到队列中
从队中拿出结点,如果该结点的左右结点不为空,就分别把左右结点加入到队列中
重复以上操作直到队列为空
"""
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def hierarchical_traversal(root):
if root is None:
return []
# 用一个list和一个索引index模拟队列的先进先出
queue = []
index = 0
result = []
queue.append(root)
while len(queue) > index:
temp = queue[index]
index += 1
result.append(temp.val)
if temp.left is not None:
queue.append(temp.left)
if temp.right is not None:
queue.append(temp.right)
return result
if __name__ == '__main__':
from Tree.tree import construct_tree
root = construct_tree()
print(hierarchical_traversal(root))