-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path102-binary_tree_level_order_traversal.py
44 lines (36 loc) · 1.3 KB
/
102-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
36
37
38
39
40
41
42
43
44
"""
https://leetcode.com/problems/binary-tree-level-order-traversal/submissions/
A standard binary tree traversal using BFS, aka in order traversal.
Runtime:
Runtime: 16 ms, faster than 97.73% of Python online submissions for Binary Tree Level Order Traversal.
Memory Usage: 13.1 MB, less than 5.88% of Python online submissions for Binary Tree Level Order Traversal.
"""
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
queue = deque()
queue.append(root)
result = []
#check for empty tree
if root == None:
return result
while queue:
nodes = [] #each "level"
for _ in range(len(queue)):
node = queue.popleft()
nodes.append(node.val)
if node.left: #check for left child
queue.append(node.left)
if node.right: #check for right child
queue.append(node.right)
result.append(nodes)
return result