Skip to content

Commit 395fdb8

Browse files
authored
Create 102-Binary-Tree-Level-Order-Traversal.py
1 parent 949f496 commit 395fdb8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def levelOrder(self, root: TreeNode) -> List[List[int]]:
10+
res = []
11+
q = collections.deque()
12+
if root: q.append(root)
13+
14+
while q:
15+
val = []
16+
17+
for i in range(len(q)):
18+
node = q.popleft()
19+
val.append(node.val)
20+
if node.left: q.append(node.left)
21+
if node.right: q.append(node.right)
22+
res.append(val)
23+
return res

0 commit comments

Comments
 (0)