-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevelOrder.py
More file actions
36 lines (34 loc) · 892 Bytes
/
Copy pathlevelOrder.py
File metadata and controls
36 lines (34 loc) · 892 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
28
29
30
31
32
33
34
35
36
#!/user/bin/env python
# coding=utf-8
"""
@project : algorithmPython
@ide : PyCharm
@file : levelOrder
@author : illusion
@desc :
@create : 2020-07-19 20:01:03
"""
# 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: TreeNode) -> [[int]]:
result = []
if not root:
return result
queue = [root]
while len(queue):
list = []
for i in range(len(queue)):
level = queue[0]
queue = queue[1:]
list.append(level.val)
if level.left:
queue.append(level.left)
if level.right:
queue.append(level.right)
result.append(list)
return result