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
- Put the root in a queue.
- Take the front node, visit it, and add its left then right child to the back of the queue.
- 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…