What is Level-order Traversal?
Level-order traversal visits a tree one depth level at a time — every node at depth 0 (the root), then every node at depth 1, then every node at depth 2, and so on, left to right within each level. It's also called Breadth-First Search (BFS) on a tree, and unlike pre-order, in-order, and post-order, it isn't defined recursively — it's naturally iterative, built around a queue instead of the call stack.
How Does It Work?
The algorithm starts by enqueueing the root. Then it repeatedly dequeues the front node, visits it, and enqueues that node's children (left, then right). Because a queue is FIFO, nodes come back out in exactly the order they were discovered — which guarantees an entire level finishes before the next one starts.
- Enqueue the root, 8. Queue: [8]
- Dequeue 8, visit it, enqueue its children 3 and 10. Queue: [3, 10]
- Dequeue 3, visit it, enqueue its children 1 and 6. Queue: [10, 1, 6]
- Dequeue 10, visit it — it has no children. Queue: [1, 6]
- Dequeue 1, visit it — it has no children. Queue: [6]
- Dequeue 6, visit it — it has no children. Queue is now empty, traversal ends
- Final sequence: [8, 3, 10, 1, 6] — notice level 0, then all of level 1, then all of level 2
This queue-driven, level-by-level structure is why BFS is the traversal used whenever "closer" needs to mean something concrete: finding the shortest path in an unweighted tree/graph, computing a node's minimum depth, or serializing a tree in a way that's easy to reconstruct row by row (e.g. printing a tree layer by layer for display).
Algorithm Steps
- If the root is null, return immediately — there's nothing to traverse
- Create a queue and enqueue the root
- While the queue isn't empty, repeat:
- Dequeue the front node and visit it
- If it has a left child, enqueue it
- If it has a right child, enqueue it
Time Complexity
- Time Complexity: Every node is enqueued and dequeued exactly once → O(n).
- Space Complexity: The queue can hold up to a full level's worth of nodes → O(w), up to O(n) worst case.
Time Complexity Analysis
Level-order needs O(w) extra space for the queue, where w is the maximum width of the tree (the largest number of nodes at any single level) — this can be as large as O(n) for a wide, shallow tree, unlike the O(h) stack space used by the recursive DFS traversals.