-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTreeZigzag.py
executable file
·56 lines (45 loc) · 1.17 KB
/
BinaryTreeZigzag.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
__author__ = 'fafu'
from Meta import TreeNode
class Solution:
def zigzagLevelOrder(self, root):
if root is None:
return []
if root.left is None and root.right is None:
return [[root.val]]
result = []
stack = [root]
order = True
while len(stack)>0:
if order:
result.append([node.val for node in stack])
order = False
else:
result.append([node.val for node in stack])
result[-1].reverse()
order = True
stack2 = []
for i in stack:
if i.left is not None:
stack2.append(i.left)
if i.right is not None:
stack2.append(i.right)
stack = stack2
return result
n1 = TreeNode(1)
n2 = TreeNode(2)
n3 = TreeNode(3)
n4 = TreeNode(4)
n5 = TreeNode(5)
n6 = TreeNode(6)
n7 = TreeNode(7)
n8 = TreeNode(8)
n1.left = n2
n1.right = n3
n2.left = n4
n2.right = n5
n3.left = n6
n3.right = n7
n4.right = n8
s = Solution()
n = s.zigzagLevelOrder(n1)
print n