-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.py
34 lines (31 loc) · 859 Bytes
/
solution.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
#!/usr/bin/env python
# Created by Bruce yuan on 18-2-6.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if root is None:
return []
import queue
q = queue.Queue()
q.put(root)
result = []
while not q.empty():
tmp = []
for _ in range(q.qsize()):
item = q.get()
tmp.append(item.val)
if item.left is not None:
q.put(item.left)
if item.right is not None:
q.put(item.right)
result.append(tmp)
return result