-
Notifications
You must be signed in to change notification settings - Fork 0
/
0103__binary_tree_zigzag_level_order_traversal.py
70 lines (50 loc) · 1.71 KB
/
0103__binary_tree_zigzag_level_order_traversal.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
LeetCode: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/
Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to
right, then right to left for the next level and alternate between).
## Example 1
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[20,9],[15,7]]
## Example 2:
Input: root = [1]
Output: [[1]]
## Example 3:
Input: root = []
Output: []
## Constraints
* The number of nodes in the tree is in the range [0, 2000].
* -100 <= Node.val <= 100
"""
from collections import deque
from typing import Optional, List
from unittest import TestCase
from lib.TreeNode import TreeNode, build_tree
class Solution(TestCase):
def test_example_1(self):
root = build_tree([3, 9, 20, None, None, 15, 7])
expected = [[3], [20, 9], [15, 7]]
self.assertEqual(expected, self.zigzagLevelOrder(root))
def test_example_2(self):
self.assertEqual([[1]], self.zigzagLevelOrder(build_tree([1])))
def test_example_3(self):
self.assertEqual([], self.zigzagLevelOrder(None))
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
q = deque([root])
result = []
reverse = False
while q:
level = []
for _ in range(len(q)):
node = q.popleft()
level.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
if reverse:
level = level[::-1]
reverse = not reverse
result.append(level)
return result