[
tree
bfs
]
BinarySearch 0070 Level Order Traversal
Problem statement
https://binarysearch.com/problems/Level-Order-Traversal/
Solution
Equal to Leetcode 0102. Binary Tree Level Order Traversal.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def solve(self, root):
if not root: return []
queue = deque([root])
result = []
while queue:
level = []
for i in range(len(queue)):
node = queue.popleft()
level.append(node.val)
if node.left: queue.append(node.left)
if node.right: queue.append(node.right)
result += level
return result