We use a Breadth-First Search (BFS) approach with a queue. For each level, we record the number of nodes currently in the queue to process that entire level before moving to the next.
- If
rootis None, return an empty list. - Initialize a queue with the
root. - While the queue is not empty:
- Get the number of nodes at the current level.
- Initialize an empty list
level_nodes. - For
sizetimes:- Pop node from queue.
- Add
node.valtolevel_nodes. - Add children to queue if they exist.
- Append
level_nodesto the finalresult.
- Return
result.
- Time Complexity: O(N).
- Space Complexity: O(W), where W is the maximum width of the tree.
from collections import deque
def level_order(root):
if not root:
return []
result = []
queue = deque([root])
while queue:
level_size = len(queue)
level_vals = []
for _ in range(level_size):
node = queue.popleft()
level_vals.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
result.append(level_vals)
return result