-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path103. Binary Tree Zigzag Level Order Traversal
More file actions
49 lines (41 loc) · 1.37 KB
/
103. Binary Tree Zigzag Level Order Traversal
File metadata and controls
49 lines (41 loc) · 1.37 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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def zigzagLevelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
queueLeft = [root]
queueRight = []
temp = []
result = []
if not root:
return []
if root == []:
return []
while queueLeft or queueRight:
while queueLeft != []:
if queueLeft[0].left:
queueRight.append(queueLeft[0].left)
if queueLeft[0].right:
queueRight.append(queueLeft[0].right)
temp.append(queueLeft.pop(0).val)
if temp != []:
result.append(temp)
temp = []
while queueRight != []:
if queueRight[0].left:
queueLeft.append(queueRight[0].left)
if queueRight[0].right:
queueLeft.append(queueRight[0].right)
temp.append(queueRight.pop(0).val)
if temp != []:
temp.reverse()
result.append(temp)
temp = []
return result