Breadth-First Search Pattern: Template + 233 LeetCode Problems
Expand outward level by level, so the first time you arrive is the shortest way.
- 23 Easy
- 148 Medium
- 62 Hard
- O(V + E) time
What the breadth-first search pattern is
Breadth-first search visits every node at distance k before any node at distance k+1, and that ordering is the entire reason to prefer it: on an unweighted graph the first time BFS reaches a node is guaranteed to be by a shortest path, so the search can stop the moment the target appears. Distance is tracked either by carrying it in the queue alongside the node, or by draining the queue one full level at a time — the level form is what you want when the answer is per-level, such as a right-side view or the maximum value on each row. Mark nodes as seen when they are enqueued, not when they are dequeued, or the same node enters the queue once per incoming edge and the cost stops being linear. The multi-source variant is worth recognising on sight: seeding the queue with every starting cell at once computes the distance from the nearest source for every cell in one pass, which is how rotting oranges and nearest-zero problems avoid running a separate BFS per source.
When to use it
- The question is the minimum number of steps, moves, or transformations.
- The graph is unweighted — with weights, this becomes Dijkstra and needs a heap.
- The answer is per level: a level order, a right-side view, a level sum.
- Distance from the nearest of several sources is needed, which one multi-source pass gives you.
The breadth-first search template in Python
The shape, not a solution to any one problem. Adapt the condition and the summary being maintained; the skeleton stays the same across the 233 problems listed below.
from collections import deque
def shortest_steps(graph, start, goal):
seen = {start}
queue = deque([(start, 0)])
while queue:
node, steps = queue.popleft()
if node == goal:
return steps # first arrival is the shortest one
for neighbour in graph[node]:
if neighbour not in seen:
seen.add(neighbour) # mark on ENQUEUE or the queue duplicates
queue.append((neighbour, steps + 1))
return -1Complexity characteristics
- Time
- O(V + E)
- Auxiliary space
- O(V)
The same linear traversal cost as depth-first search: the difference is the order of the visits, not the amount of work. The queue holds the widest level rather than the deepest path, which on a wide graph or a filled grid is O(V) and can be considerably more memory than DFS uses on the same input. A multi-source pass costs the same single traversal however many sources are seeded, which is the whole reason to prefer it to one BFS per source.
All 233 breadth-first search LeetCode problems
Every problem in the library the breadth-first search pattern applies to, grouped by LeetCode's own difficulty rating. 185 of the 233 carry a complete Python solution with a worked example and complexity analysis; the rest are listed for completeness, with the LeetCode Premium ones marked.
Easy (23)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 100 | Same Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 101 | Symmetric Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 104 | Maximum Depth of Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 111 | Minimum Depth of Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 112 | Path Sum | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 226 | Invert Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 404 | Sum of Left Leaves | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 463 | Island Perimeter | Easy | Depth-First Search, Breadth-First Search, Array +1 |
| 530 | Minimum Absolute Difference in BST | Easy | Tree, Depth-First Search, Breadth-First Search +2 |
| 559 | Maximum Depth of N-ary Tree | Easy | Tree, Depth-First Search, Breadth-First Search |
| 617 | Merge Two Binary Trees | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 637 | Average of Levels in Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 653 | Two Sum IV - Input is a BST | Easy | Tree, Depth-First Search, Breadth-First Search +4 |
| 733 | Flood Fill | Easy | Depth-First Search, Breadth-First Search, Array +1 |
| 933 | Number of Recent Calls | Easy | Design, Queue, Data Stream |
| 346 | Moving Average from Data StreamPremium | Easy | Design, Queue, Array +1 |
| 783 | Minimum Distance Between BST Nodes | Easy | Tree, Depth-First Search, Breadth-First Search +2 |
| 965 | Univalued Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 993 | Cousins in Binary Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 1379 | Find a Corresponding Node of a Binary Tree in a Clone of That Tree | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 1469 | Find All The Lonely NodesPremium | Easy | Tree, Depth-First Search, Breadth-First Search +1 |
| 1971 | Find if Path Exists in Graph | Easy | Depth-First Search, Breadth-First Search, Union Find +1 |
| 2073 | Time Needed to Buy Tickets | Easy | Queue, Array, Simulation |
Medium (148)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 102 | Binary Tree Level Order Traversal | Medium | Tree, Breadth-First Search, Binary Tree |
| 103 | Binary Tree Zigzag Level Order Traversal | Medium | Tree, Breadth-First Search, Binary Tree |
| 107 | Binary Tree Level Order Traversal II | Medium | Tree, Breadth-First Search, Binary Tree |
| 116 | Populating Next Right Pointers in Each Node | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 117 | Populating Next Right Pointers in Each Node II | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 130 | Surrounded Regions | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 133 | Clone Graph | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 199 | Binary Tree Right Side View | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 200 | Number of Islands | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 207 | Course Schedule | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 210 | Course Schedule II | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 279 | Perfect Squares | Medium | Breadth-First Search, Math, Dynamic Programming |
| 310 | Minimum Height Trees | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 322 | Coin Change | Medium | Breadth-First Search, Array, Dynamic Programming |
| 365 | Water and Jug Problem | Medium | Depth-First Search, Breadth-First Search, Math |
| 399 | Evaluate Division | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 417 | Pacific Atlantic Water Flow | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 429 | N-ary Tree Level Order Traversal | Medium | Tree, Breadth-First Search |
| 433 | Minimum Genetic Mutation | Medium | Breadth-First Search, Hash Table, String |
| 449 | Serialize and Deserialize BST | Medium | Tree, Depth-First Search, Breadth-First Search +4 |
| 513 | Find Bottom Left Tree Value | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 515 | Find Largest Value in Each Tree Row | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 529 | Minesweeper | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 542 | 01 Matrix | Medium | Breadth-First Search, Array, Dynamic Programming +1 |
| 547 | Number of Provinces | Medium | Depth-First Search, Breadth-First Search, Union Find +1 |
| 622 | Design Circular Queue | Medium | Design, Queue, Array +1 |
| 623 | Add One Row to Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 641 | Design Circular Deque | Medium | Design, Queue, Array +1 |
| 655 | Print Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 662 | Maximum Width of Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 672 | Bulb Switcher II | Medium | Bit Manipulation, Depth-First Search, Breadth-First Search +1 |
| 684 | Redundant Connection | Medium | Depth-First Search, Breadth-First Search, Union Find +1 |
| 690 | Employee Importance | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 695 | Max Area of Island | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 721 | Accounts Merge | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 743 | Network Delay Time | Medium | Depth-First Search, Breadth-First Search, Graph +2 |
| 787 | Cheapest Flights Within K Stops | Medium | Depth-First Search, Breadth-First Search, Graph +3 |
| 841 | Keys and Rooms | Medium | Depth-First Search, Breadth-First Search, Graph |
| 909 | Snakes and Ladders | Medium | Breadth-First Search, Array, Matrix |
| 994 | Rotting Oranges | Medium | Breadth-First Search, Array, Matrix |
| 1161 | Maximum Level Sum of a Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1448 | Count Good Nodes in Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1466 | Reorder Routes to Make All Paths Lead to the City Zero | Medium | Depth-First Search, Breadth-First Search, Graph |
| 1926 | Nearest Exit from Entrance in Maze | Medium | Breadth-First Search, Array, Matrix |
| 261 | Graph Valid TreePremium | Medium | Depth-First Search, Breadth-First Search, Union Find +1 |
| 281 | Zigzag IteratorPremium | Medium | Design, Queue, Array +1 |
| 286 | Walls and GatesPremium | Medium | Breadth-First Search, Array, Matrix |
| 314 | Binary Tree Vertical Order TraversalPremium | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 323 | Number of Connected Components in an Undirected GraphPremium | Medium | Depth-First Search, Breadth-First Search, Union Find +1 |
| 339 | Nested List Weight SumPremium | Medium | Depth-First Search, Breadth-First Search |
| 364 | Nested List Weight Sum IIPremium | Medium | Stack, Depth-First Search, Breadth-First Search |
| 490 | The MazePremium | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 505 | The Maze IIPremium | Medium | Depth-First Search, Breadth-First Search, Graph +4 |
| 582 | Kill ProcessPremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 694 | Number of Distinct IslandsPremium | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 737 | Sentence Similarity IIPremium | Medium | Depth-First Search, Breadth-First Search, Union Find +3 |
| 742 | Closest Leaf in a Binary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 752 | Open the Lock | Medium | Breadth-First Search, Array, Hash Table +1 |
| 785 | Is Graph Bipartite? | Medium | Depth-First Search, Breadth-First Search, Union Find +1 |
| 797 | All Paths From Source to Target | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 802 | Find Eventual Safe States | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 863 | All Nodes Distance K in Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 865 | Smallest Subtree with all the Deepest Nodes | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 886 | Possible Bipartition | Medium | Depth-First Search, Breadth-First Search, Union Find +1 |
| 919 | Complete Binary Tree Inserter | Medium | Tree, Breadth-First Search, Design +1 |
| 934 | Shortest Bridge | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 950 | Reveal Cards In Increasing Order | Medium | Queue, Array, Sorting +1 |
| 958 | Check Completeness of a Binary Tree | Medium | Tree, Breadth-First Search, Binary Tree |
| 959 | Regions Cut By Slashes | Medium | Depth-First Search, Breadth-First Search, Union Find +3 |
| 967 | Numbers With Same Consecutive Differences | Medium | Breadth-First Search, Backtracking |
| 1020 | Number of Enclaves | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 1034 | Coloring A Border | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 1042 | Flower Planting With No Adjacent | Medium | Depth-First Search, Breadth-First Search, Graph |
| 1087 | Brace ExpansionPremium | Medium | Stack, Breadth-First Search, String +2 |
| 1091 | Shortest Path in Binary Matrix | Medium | Breadth-First Search, Array, Matrix |
| 1102 | Path With Maximum Minimum ValuePremium | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1123 | Lowest Common Ancestor of Deepest Leaves | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1129 | Shortest Path with Alternating Colors | Medium | Breadth-First Search, Graph |
| 1162 | As Far from Land as Possible | Medium | Breadth-First Search, Array, Dynamic Programming +1 |
| 1197 | Minimum Knight MovesPremium | Medium | Breadth-First Search |
| 1202 | Smallest String With Swaps | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1215 | Stepping NumbersPremium | Medium | Breadth-First Search, Math, Backtracking |
| 1236 | Web CrawlerPremium | Medium | Depth-First Search, Breadth-First Search, String +1 |
| 1242 | Web Crawler MultithreadedPremium | Medium | Depth-First Search, Breadth-First Search, Concurrency |
| 1245 | Tree DiameterPremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1254 | Number of Closed Islands | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 1257 | Smallest Common RegionPremium | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 1261 | Find Elements in a Contaminated Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 1267 | Count Servers that Communicate | Medium | Depth-First Search, Breadth-First Search, Union Find +3 |
| 1273 | Delete Tree NodesPremium | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1302 | Deepest Leaves Sum | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1306 | Jump Game III | Medium | Depth-First Search, Breadth-First Search, Array |
| 1311 | Get Watched Videos by Your Friends | Medium | Breadth-First Search, Graph, Array +2 |
| 1315 | Sum of Nodes with Even-Valued Grandparent | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1319 | Number of Operations to Make Network Connected | Medium | Depth-First Search, Breadth-First Search, Union Find +1 |
| 1361 | Validate Binary Tree Nodes | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 1376 | Time Needed to Inform All Employees | Medium | Tree, Depth-First Search, Breadth-First Search |
| 1391 | Check if There is a Valid Path in a Grid | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 1430 | Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1443 | Minimum Time to Collect All Apples in a Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1457 | Pseudo-Palindromic Paths in a Binary Tree | Medium | Bit Manipulation, Tree, Depth-First Search +2 |
| 1462 | Course Schedule IV | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 1485 | Clone Binary Tree With Random PointerPremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1490 | Clone N-ary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 1519 | Number of Nodes in the Sub-Tree With the Same Label | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1559 | Detect Cycles in 2D Grid | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 1602 | Find Nearest Right Node in Binary TreePremium | Medium | Tree, Breadth-First Search, Binary Tree |
| 1609 | Even Odd Tree | Medium | Tree, Breadth-First Search, Binary Tree |
| 1625 | Lexicographically Smallest String After Applying Operations | Medium | Depth-First Search, Breadth-First Search, String +1 |
| 1631 | Path With Minimum Effort | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1654 | Minimum Jumps to Reach Home | Medium | Breadth-First Search, Array, Dynamic Programming |
| 1660 | Correct a Binary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1670 | Design Front Middle Back Queue | Medium | Design, Queue, Array +2 |
| 1730 | Shortest Path to Get FoodPremium | Medium | Breadth-First Search, Array, Matrix |
| 1740 | Find Distance in a Binary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 1765 | Map of Highest Peak | Medium | Breadth-First Search, Array, Matrix |
| 1778 | Shortest Path in a Hidden GridPremium | Medium | Depth-First Search, Breadth-First Search, Array +2 |
| 1810 | Minimum Path Cost in a Hidden GridPremium | Medium | Depth-First Search, Breadth-First Search, Graph +5 |
| 1823 | Find the Winner of the Circular Game | Medium | Recursion, Queue, Array +2 |
| 1905 | Count Sub Islands | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 1992 | Find All Groups of Farmland | Medium | Depth-First Search, Breadth-First Search, Array +1 |
| 1993 | Operations on Tree | Medium | Tree, Depth-First Search, Breadth-First Search +3 |
| 2039 | The Time When the Network Becomes Idle | Medium | Breadth-First Search, Graph, Array |
| 2059 | Minimum Operations to Convert Number | Medium | Breadth-First Search, Array |
| 2101 | Detonate the Maximum Bombs | Medium | Depth-First Search, Breadth-First Search, Graph +3 |
| 2146 | K Highest Ranked Items Within a Price Range | Medium | Breadth-First Search, Array, Matrix +2 |
| 2174 | Remove All Ones With Row and Column Flips IIPremium | Medium | Bit Manipulation, Breadth-First Search, Array +1 |
| 2192 | All Ancestors of a Node in a Directed Acyclic Graph | Medium | Depth-First Search, Breadth-First Search, Graph +1 |
| 2316 | Count Unreachable Pairs of Nodes in an Undirected Graph | Medium | Depth-First Search, Breadth-First Search, Union Find +1 |
| 2368 | Reachable Nodes With Restrictions | Medium | Tree, Depth-First Search, Breadth-First Search +4 |
| 2385 | Amount of Time for Binary Tree to Be Infected | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 2415 | Reverse Odd Levels of Binary Tree | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 2445 | Number of Nodes With Value OnePremium | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 2467 | Most Profitable Path in a Tree | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 2471 | Minimum Number of Operations to Sort a Binary Tree by Level | Medium | Tree, Breadth-First Search, Binary Tree |
| 2477 | Minimum Fuel Cost to Report to the Capital | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 2492 | Minimum Score of a Path Between Two Cities | Medium | Depth-First Search, Breadth-First Search, Union Find +1 |
| 2556 | Disconnect Path in a Binary Matrix by at Most One Flip | Medium | Depth-First Search, Breadth-First Search, Array +2 |
| 2583 | Kth Largest Sum in a Binary Tree | Medium | Tree, Breadth-First Search, Binary Tree +1 |
| 2596 | Check Knight Tour Configuration | Medium | Depth-First Search, Breadth-First Search, Array +2 |
| 2641 | Cousins in Binary Tree II | Medium | Tree, Depth-First Search, Breadth-First Search +2 |
| 2658 | Maximum Number of Fish in a Grid | Medium | Depth-First Search, Breadth-First Search, Union Find +2 |
| 2685 | Count the Number of Complete Components | Medium | Depth-First Search, Breadth-First Search, Union Find +1 |
| 2773 | Height of Special Binary TreePremium | Medium | Tree, Depth-First Search, Breadth-First Search +1 |
| 2812 | Find the Safest Path in a Grid | Medium | Breadth-First Search, Union Find, Array +3 |
| 2850 | Minimum Moves to Spread Stones Over Grid | Medium | Breadth-First Search, Array, Dynamic Programming +1 |
| 2852 | Sum of Remoteness of All CellsPremium | Medium | Depth-First Search, Breadth-First Search, Union Find +3 |
| 2998 | Minimum Number of Operations to Make X and Y Equal | Medium | Breadth-First Search, Memoization, Dynamic Programming |
Hard (62)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 126 | Word Ladder II | Hard | Breadth-First Search, Hash Table, String +1 |
| 127 | Word Ladder | Hard | Breadth-First Search, Hash Table, String |
| 297 | Serialize and Deserialize Binary Tree | Hard | Tree, Depth-First Search, Breadth-First Search +3 |
| 301 | Remove Invalid Parentheses | Hard | Breadth-First Search, String, Backtracking |
| 329 | Longest Increasing Path in a Matrix | Hard | Depth-First Search, Breadth-First Search, Graph +5 |
| 407 | Trapping Rain Water II | Hard | Breadth-First Search, Array, Matrix +1 |
| 488 | Zuma Game | Hard | Stack, Breadth-First Search, Memoization +2 |
| 514 | Freedom Trail | Hard | Depth-First Search, Breadth-First Search, String +1 |
| 675 | Cut Off Trees for Golf Event | Hard | Breadth-First Search, Array, Matrix +1 |
| 685 | Redundant Connection II | Hard | Depth-First Search, Breadth-First Search, Union Find +1 |
| 749 | Contain Virus | Hard | Depth-First Search, Breadth-First Search, Array +2 |
| 778 | Swim in Rising Water | Hard | Depth-First Search, Breadth-First Search, Union Find +4 |
| 269 | Alien DictionaryPremium | Hard | Depth-First Search, Breadth-First Search, Graph +3 |
| 302 | Smallest Rectangle Enclosing Black PixelsPremium | Hard | Depth-First Search, Breadth-First Search, Array +2 |
| 317 | Shortest Distance from All BuildingsPremium | Hard | Breadth-First Search, Array, Matrix |
| 428 | Serialize and Deserialize N-ary TreePremium | Hard | Tree, Depth-First Search, Breadth-First Search +1 |
| 431 | Encode N-ary Tree to Binary TreePremium | Hard | Tree, Depth-First Search, Breadth-First Search +2 |
| 499 | The Maze IIIPremium | Hard | Depth-First Search, Breadth-First Search, Graph +5 |
| 711 | Number of Distinct Islands IIPremium | Hard | Depth-First Search, Breadth-First Search, Union Find +2 |
| 765 | Couples Holding Hands | Hard | Greedy, Depth-First Search, Breadth-First Search +2 |
| 773 | Sliding Puzzle | Hard | Breadth-First Search, Memoization, Array +3 |
| 815 | Bus Routes | Hard | Breadth-First Search, Array, Hash Table |
| 827 | Making A Large Island | Hard | Depth-First Search, Breadth-First Search, Union Find +2 |
| 839 | Similar String Groups | Hard | Depth-First Search, Breadth-First Search, Union Find +3 |
| 847 | Shortest Path Visiting All Nodes | Hard | Bit Manipulation, Breadth-First Search, Graph +2 |
| 854 | K-Similar Strings | Hard | Breadth-First Search, Hash Table, String |
| 864 | Shortest Path to Get All Keys | Hard | Bit Manipulation, Breadth-First Search, Array +1 |
| 924 | Minimize Malware Spread | Hard | Depth-First Search, Breadth-First Search, Union Find +3 |
| 928 | Minimize Malware Spread II | Hard | Depth-First Search, Breadth-First Search, Union Find +3 |
| 987 | Vertical Order Traversal of a Binary Tree | Hard | Tree, Depth-First Search, Breadth-First Search +3 |
| 1036 | Escape a Large Maze | Hard | Depth-First Search, Breadth-First Search, Array +1 |
| 1096 | Brace Expansion II | Hard | Stack, Breadth-First Search, Hash Table +3 |
| 1203 | Sort Items by Groups Respecting Dependencies | Hard | Depth-First Search, Breadth-First Search, Graph +1 |
| 1210 | Minimum Moves to Reach Target with Rotations | Hard | Breadth-First Search, Array, Matrix |
| 1263 | Minimum Moves to Move a Box to Their Target Location | Hard | Breadth-First Search, Array, Matrix +1 |
| 1284 | Minimum Number of Flips to Convert Binary Matrix to Zero Matrix | Hard | Bit Manipulation, Breadth-First Search, Array +2 |
| 1293 | Shortest Path in a Grid with Obstacles Elimination | Hard | Breadth-First Search, Array, Matrix |
| 1298 | Maximum Candies You Can Get from Boxes | Hard | Breadth-First Search, Graph, Array |
| 1345 | Jump Game IV | Hard | Breadth-First Search, Array, Hash Table |
| 1368 | Minimum Cost to Make at Least One Valid Path in a Grid | Hard | Breadth-First Search, Graph, Array +3 |
| 1377 | Frog Position After T Seconds | Hard | Tree, Depth-First Search, Breadth-First Search +1 |
| 1483 | Kth Ancestor of a Tree Node | Hard | Bit Manipulation, Tree, Depth-First Search +4 |
| 1568 | Minimum Number of Days to Disconnect Island | Hard | Depth-First Search, Breadth-First Search, Array +2 |
| 1970 | Last Day Where You Can Still Cross | Hard | Depth-First Search, Breadth-First Search, Union Find +3 |
| 2045 | Second Minimum Time to Reach Destination | Hard | Breadth-First Search, Graph, Shortest Path |
| 2092 | Find All People With Secret | Hard | Depth-First Search, Breadth-First Search, Union Find +2 |
| 2204 | Distance to a Cycle in Undirected GraphPremium | Hard | Depth-First Search, Breadth-First Search, Union Find +1 |
| 2258 | Escape the Spreading Fire | Hard | Breadth-First Search, Array, Binary Search +1 |
| 2277 | Closest Node to Path in TreePremium | Hard | Tree, Depth-First Search, Breadth-First Search +1 |
| 2290 | Minimum Obstacle Removal to Reach Corner | Hard | Breadth-First Search, Graph, Array +3 |
| 2328 | Number of Increasing Paths in a Grid | Hard | Depth-First Search, Breadth-First Search, Graph +5 |
| 2360 | Longest Cycle in a Graph | Hard | Depth-First Search, Breadth-First Search, Graph +1 |
| 2458 | Height of Binary Tree After Subtree Removal Queries | Hard | Tree, Depth-First Search, Breadth-First Search +2 |
| 2493 | Divide Nodes Into the Maximum Number of Groups | Hard | Depth-First Search, Breadth-First Search, Union Find +1 |
| 2503 | Maximum Number of Points From Grid Queries | Hard | Breadth-First Search, Union Find, Array +4 |
| 2534 | Time Taken to Cross the DoorPremium | Hard | Queue, Array, Simulation |
| 2577 | Minimum Time to Visit a Cell In a Grid | Hard | Breadth-First Search, Graph, Array +3 |
| 2608 | Shortest Cycle in a Graph | Hard | Breadth-First Search, Graph |
| 2612 | Minimum Reverse Operations | Hard | Breadth-First Search, Union Find, Array +2 |
| 2617 | Minimum Number of Visited Cells in a Grid | Hard | Stack, Breadth-First Search, Union Find +5 |
| 2814 | Minimum Time Takes to Reach Destination Without DrowningPremium | Hard | Breadth-First Search, Array, Matrix |
| 2858 | Minimum Edge Reversals So Every Node Is Reachable | Hard | Depth-First Search, Breadth-First Search, Graph +1 |
Related patterns
Problems sit in more than one pattern more often than not, and the overlap is where the interesting follow-up questions live.
Breadth-First Search pattern FAQ
What is the breadth-first search pattern?
Breadth-first search visits every node at distance k before any node at distance k+1, and that ordering is the entire reason to prefer it: on an unweighted graph the first time BFS reaches a node is guaranteed to be by a shortest path, so the search can stop the moment the target appears.
How many LeetCode problems use the breadth-first search pattern?
This page lists 233 LeetCode problems that the breadth-first search pattern applies to: 23 Easy, 148 Medium and 62 Hard. 185 of them carry a complete Python solution with complexity analysis.
What is the time complexity of the breadth-first search pattern?
O(V + E) time and O(V) space. The same linear traversal cost as depth-first search: the difference is the order of the visits, not the amount of work. The queue holds the widest level rather than the deepest path, which on a wide graph or a filled grid is O(V) and can be considerably more memory than DFS uses on the same input. A multi-source pass costs the same single traversal however many sources are seeded, which is the whole reason to prefer it to one BFS per source.
When should I use the breadth-first search pattern in an interview?
The question is the minimum number of steps, moves, or transformations. The graph is unweighted — with weights, this becomes Dijkstra and needs a heap.
Which breadth-first search problem should I start with?
LeetCode 100. Same Tree is the lowest-numbered Easy problem on this page, which makes it the usual starting point: the technique is visible without the problem's own complications getting in the way.
What patterns are related to breadth-first search?
Depth-First Search, Matrix and Grid, Topological Sort, Heap / Priority Queue. Problems frequently sit in more than one of these, and the overlap is where the interesting follow-up questions come from.
More ways in: all 22 patterns, the curated study lists, or the full problem list.
Meet the breadth-first search problem you did not practise
Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.