BFS / DFS
Breadth-first search explores level by level (shortest path in an unweighted graph); depth-first search goes deep first (reachability, connected components, backtracking). Both visit every node once — the choice is about order, not coverage.
27 LeetCode problems solved with the BFS / DFS pattern. Practice them with spaced repetition so the pattern sticks.
- 01 MatrixMEDIUM · O(m * n)
- Average of Levels in Binary TreeEASY · O(n)
- Binary Tree Level Order TraversalMEDIUM · O(n)
- Binary Tree Level Order Traversal IIMEDIUM · O(n)
- Binary Tree Right Side ViewMEDIUM · O(n)
- Binary Tree Zigzag Level Order TraversalMEDIUM · O(n)
- Find Bottom Left Tree ValueMEDIUM · O(n)
- Find Largest Value in Each Tree RowMEDIUM · O(n)
- Flood FillEASY · O(m * n)
- Keys and RoomsMEDIUM · O(n + e)
- Max Area of IslandMEDIUM · O(m*n)
- Maximum Level Sum of a Binary TreeMEDIUM · O(n)
- Minimum Depth of Binary TreeEASY · O(n)
- Minimum Genetic MutationMEDIUM · O(B * L * 4)
- N-ary Tree Level Order TraversalMEDIUM · O(n)
- Nearest Exit from Entrance in MazeMEDIUM · O(m * n)
- Number of IslandsMEDIUM · O(m*n)
- Open the LockMEDIUM · O(N^2 * A^N + D)
- Pacific Atlantic Water FlowMEDIUM · O(m*n)
- Path SumEASY · O(n)
- Populating Next Right Pointers in Each NodeMEDIUM · O(n)
- Reorder Routes to Make All Paths Lead to the City ZeroMEDIUM · O(n)
- Rotting OrangesMEDIUM · O(m*n)
- Snakes and LaddersMEDIUM · O(n^2)
- Surrounded RegionsMEDIUM · O(m * n)
- Walls and GatesMEDIUM · O(m*n)
- Word LadderHARD · O(N * L^2)
See the full solution
The complete approach, reference solutions in 5 languages, and a step-by-step visualization — then add this problem to your spaced-repetition schedule.
Start free →