-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
按之字形顺序打印二叉树.py
79 lines (74 loc) · 2.38 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'''
请实现一个函数按照之字形打印二叉树,
即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
'''
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# 存储点的时候按照奇数层和偶数层分别存储
def Print(self, pRoot):
if not pRoot:
return []
result, nodes = [], [pRoot]
right = True
while nodes:
curStack, nextStack = [], []
if right:
for node in nodes:
curStack.append(node.val)
if node.left:
nextStack.append(node.left)
if node.right:
nextStack.append(node.right)
else:
for node in nodes:
curStack.append(node.val)
if node.right:
nextStack.append(node.right)
if node.left:
nextStack.append(node.left)
nextStack.reverse()
right = not right
result.append(curStack)
nodes = nextStack
return result
# 转换思路,存储的时候一直从左向右存储,打印的时候根据不同的层一次打印
def zigzagLevelOrder(self, root):
if not root:
return []
levels, result, leftToRight = [root], [], True
while levels:
curValues, nextLevel = [], []
for node in levels:
curValues.append(node.val)
if node.left:
nextLevel.append(node.left)
if node.right:
nextLevel.append(node.right)
if not leftToRight:
curValues.reverse()
if curValues:
result.append(curValues)
levels = nextLevel
leftToRight = not leftToRight
return result
pNode1 = TreeNode(8)
pNode2 = TreeNode(6)
pNode3 = TreeNode(10)
pNode4 = TreeNode(5)
pNode5 = TreeNode(7)
pNode6 = TreeNode(9)
pNode7 = TreeNode(11)
pNode1.left = pNode2
pNode1.right = pNode3
pNode2.left = pNode4
pNode2.right = pNode5
pNode3.left = pNode6
pNode3.right = pNode7
S = Solution()
aList = S.Print(pNode1)
print(aList)