forked from ScarlettCC/AlgorithmByPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintBinaryTree.py
More file actions
90 lines (88 loc) · 2.52 KB
/
PrintBinaryTree.py
File metadata and controls
90 lines (88 loc) · 2.52 KB
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
80
81
82
83
84
85
86
87
88
89
90
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def PrintByZigzag(self, pRoot):
# write code here
res, temp = [], []
if pRoot is None:
return res
temp = [pRoot]
right = True
while temp:
curstack,nextstack = [],[]
if right:
for node in temp:
curstack.append(node.val)
if node.left:
nextstack.append(node.left)
if node.right:
nextstack.append(node.right)
else:
for node in temp:
curstack.append(node.val)
if node.right:
nextstack.append(node.right)
if node.left:
nextstack.append(node.left)
nextstack.reverse()
temp = nextstack
right = not right
res.append(curstack)
return res
def PrintByZigzag2(self,pRoot):
res,temp = [],[]
if not pRoot:
return res
temp = [pRoot]
lefttoright = True
while temp:
cur, nextstack = [], []
for node in temp:
cur.append(node.val)
if node.left:
nextstack.append(node.left)
if node.right:
nextstack.append(node.right)
if not lefttoright:
cur.reverse()
res.append(cur)
temp = nextstack
lefttoright = not lefttoright
return res
def PrintToSeveralLevel(self, pRoot):
# write code here
res,temp = [],[]
if pRoot is None:
return res
temp = [pRoot]
while temp:
curstack, nextstack = [],[]
for node in temp:
curstack.append(node.val)
if node.left:
nextstack.append(node.left)
if node.right:
nextstack.append(node.right)
res.append(curstack)
temp = nextstack
return res
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.PrintByZigzag2(pNode1)
print(aList)