forked from yanglei-github/Leetcode_in_python3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path103_二叉树的锯齿形层次遍历.py
More file actions
32 lines (28 loc) · 823 Bytes
/
103_二叉树的锯齿形层次遍历.py
File metadata and controls
32 lines (28 loc) · 823 Bytes
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
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 9 13:36:22 2020
@author: leiya
"""
class Solution:
def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
count = 0
res = []
if not root:
return res
queue = [root]
while queue:
count += 1
cur_res = []
current_len = len(queue)
for _ in range(current_len):
pop_node = queue.pop(0)
cur_res.append(pop_node.val)
if pop_node.left:
queue.append(pop_node.left)
if pop_node.right:
queue.append(pop_node.right)
if count % 2 == 0:
res.append(cur_res[::-1])
else:
res.append(cur_res)
return res