-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102.py
33 lines (28 loc) · 862 Bytes
/
102.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
# 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 levelOrder(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
results = []
queue = [root]
while True:
children_queue = []
res = []
while queue:
node = queue[0]
del queue[0]
res.append(node.val)
if node.left:
children_queue.append(node.left)
if node.right:
children_queue.append(node.right)
results.append(res)
queue = children_queue
if not queue:
break
return results