visualization
Tree BFS (Level Order)
The queue is a conveyor belt: levels go in whole, come out whole.
binary tree · root 1 · levels [1] [2,3] [4,5,6,7]step 1/16
queue (front → back)
1
Seed the queue with root 1. Everything BFS will ever visit flows through this queue.
pseudocode
queue = deque([root])while queue:node = queue.popleft()visit(node)for child in (node.left, node.right):if child:queue.append(child)