-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path102.py
More file actions
27 lines (23 loc) · 705 Bytes
/
102.py
File metadata and controls
27 lines (23 loc) · 705 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
# Definition for a binary tree node.
from typing import List
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if not root:
return []
res, nodes = [], [root]
while nodes:
values, next_nodes = [], []
for node in nodes:
values.append(node.val)
if node.left:
next_nodes.append(node.left)
if node.right:
next_nodes.append(node.right)
res.append(values)
nodes = next_nodes
return res