Skip to content
StackDecode
TreesAnimated

Level Order Traversal

Visit the tree row by row using a queue.

Time

O(n)

Extra space

O(w)

Use it for

Shortest depth, breadth-first tasks.

How it works

  1. Put the root in a queue.
  2. Take the front node, visit it, and add its left then right child to the back of the queue.
  3. Repeat until the queue is empty. Because the queue is first-in-first-out, nodes are visited row by row, top to bottom.

Watch it run

Loading animation…