-
Notifications
You must be signed in to change notification settings - Fork 0
/
103.py
29 lines (25 loc) · 960 Bytes
/
103.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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return root
level_dict = {}
stack = [{'level': 0, 'node': root}]
while stack:
root = stack.pop()
node, level = root['node'], root['level']
level_dict.setdefault(level, list())
if level % 2 == 0:
level_dict[level].append(node.val)
else:
level_dict[level] = [node.val] + level_dict[level]
if node.right:
stack.append({'level': level + 1, 'node': node.right})
if node.left:
stack.append({'level': level + 1, 'node': node.left})
return list(level_dict.values())