-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path103.py
More file actions
46 lines (39 loc) · 1.21 KB
/
103.py
File metadata and controls
46 lines (39 loc) · 1.21 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
# 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]]
"""
if not root:
return []
ans, i = [], 0
que1, que2 = [root], []
while que1 or que2:
tmp = []
if i % 2 == 0:
for p in que1:
tmp.append(p.val)
for p in que1[::-1]:
if p.right:
que2.append(p.right)
if p.left:
que2.append(p.left)
que1 = []
else:
for p in que2:
tmp.append(p.val)
for p in que2[::-1]:
if p.left:
que1.append(p.left)
if p.right:
que1.append(p.right)
que2 = []
i += 1
ans.append(tmp)
return ans