LeetCode ยท BFS
BFS Problem Set
Level-order traversal, shortest path in unweighted graphs, multi-source expansion, and state-space search. If the prompt asks for the minimum number of steps, nearest distance, or simultaneous spread, BFS should be one of your first candidates.
| Difficulty | Pattern | Problem | Key Insight |
|---|---|---|---|
| Medium | flood-fill | LC 200 ยท Number of Islands | Treat each land cell as a node and flood one connected component at a time. BFS marks an entire island from each unvisited start cell. |
| Medium | multi-source | LC 994 ยท Rotting Oranges | Enqueue every rotten orange first. One BFS layer equals one minute of spread, so the number of layers gives the answer directly. |
| Medium | multi-source | LC 542 ยท 01 Matrix | Reverse the search: start from all zero cells and expand outward. The first time BFS reaches a one-cell is its nearest-zero distance. |
| Medium | grid shortest-path | LC 1091 ยท Shortest Path in Binary Matrix | Standard shortest-path BFS on an unweighted grid, but with 8 directions. Distance is the BFS level when you first reach the bottom-right cell. |
| Hard | implicit graph | LC 127 ยท Word Ladder | Each word is a node; edges connect words differing by one character. BFS finds the shortest transformation sequence in this implicit graph. |
| Hard | state-space | LC 773 ยท Sliding Puzzle | Each board arrangement is a node and each legal swap is an edge. Encode states compactly and use BFS to guarantee minimum moves. |
Practice note: All listed BFS problems on this page now have dedicated solution write-ups, so you can move from the concept page straight into a worked example for each pattern.