Reqflow
Searching·Intermediate

BFS: Level-Order Traversal

Time O(n)Space O(n)
·

Breadth-First Search explores a tree level by level using a queue. It enqueues the root, then repeatedly dequeues a node, visits it, and enqueues its children. Because nodes are processed in arrival order, BFS guarantees that when it first reaches a node it has found the shortest path from the root.

Example: Level-order traversal of a 7-node binary tree.

When to use this

See also

← All algorithms