-
Notifications
You must be signed in to change notification settings - Fork 0
/
32_3_zigzag_traverse_of_binary_tree.py
58 lines (48 loc) · 1.43 KB
/
32_3_zigzag_traverse_of_binary_tree.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
# -*- coding=utf-8 -*-
class TreeNode():
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class Solution():
"""分层之字形打印二叉树
"""
def Print(self, pRoot):
if pRoot is None:
return []
s1, s2, res = [], [], []
s1.append(pRoot)
while s1 or s2:
if s1:
cur_level_res = []
while s1:
item = s1.pop()
cur_level_res.append(item.val)
if item.left:
s2.append(item.left)
if item.right:
s2.append(item.right)
res.append(cur_level_res)
if s2:
cur_level_res = []
while s2:
item = s2.pop()
cur_level_res.append(item.val)
if item.right:
s1.append(item.right)
if item.left:
s1.append(item.left)
res.append(cur_level_res)
return res
def main():
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.right.left = TreeNode(6)
root.right.right = TreeNode(7)
ex = Solution()
print(ex.Print(root))
if __name__ == "__main__":
main()