Tree BFS / Level Order
Use a queue to visit tree nodes in layers, from the root outward.
The task asks for level order, values by depth, nearest node, or the first level satisfying a condition.
How to approach it
Push the root, then repeatedly process the current queue length and enqueue each node's children.
Queue memory can grow with the widest level; do not use a stack if exact level order matters.
Practice question
What is the level-order traversal of a tree with root 1, children 2 and 3, and node 2's right child 4?
Tree levels: [1], [2, 3], [4]Give your answer before revealing the explanation.Show a hint
Process the queue one level at a time.
Show an answer
[[1], [2, 3], [4]]
Tree BFS / Level Order at work
A building directory is a hierarchy. Starting at the lobby, list departments floor by floor, with all departments at the same depth together. Which traversal matches this?
Show one possible approach
Breadth-first search with a queue. Process exactly the current queue length for each floor/level, then process the children in the next round.
Practice next
Use these problem IDs to find additional practice for this pattern: