forked from taizilongxu/Leetcode-Py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Tree Level Order Traversal.py
35 lines (34 loc) · 1.09 KB
/
Binary Tree Level Order Traversal.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
35
class Solution:
def levelOrder(self, root):
if root is None:
return []
current, res = [root], []
while current:
next, vals = [], []
for node in current:
vals.append(node.val)
if node.left:
next.append(node.left)
if node.right:
next.append(node.right)
res.append(vals)
current = next
return res
# def levelOrder(self, root):
# """ Using a queue is also fine.
# """
# if root is None:
# return []
# current, res = [root], []
# while current:
# vals, length = [], len(current)
# for i in range(length):
# node = current[0]
# vals.append(node.val)
# if node.left:
# current.append(node.left)
# if node.right:
# current.append(node.right)
# current.pop(0)
# res.append(vals)
# return res