Breadth-First Search LeetCode Problems: All 223,
With Python Solutions

Every problem in this library that LeetCode tags Breadth-First Search. 178 of them carry a complete Python solution, a worked example and the time and space complexity of the approach.

Easy
20
Medium
142
Hard
61
1 pattern

How Breadth-First Search problems are solved

A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Breadth-First Search problems — each one explains the approach, gives a Python template and states its complexity.

Eyes only

When the Breadth-First Search problem arrives live.

In the live round, Stealth Interview reads the 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 says. It's a desktop app for macOS and Windows, invisible to screen sharing.

Breadth-First Search problems by difficulty

Showing the first 200 of 223 problems. Problems with a complete Python solution are listed first, then by ascending problem number.

Easy (19)

Easy problems
No.Problem
0100Same Tree
0101Symmetric Tree
0104Maximum Depth of Binary Tree
0111Minimum Depth of Binary Tree
0112Path Sum
0226Invert Binary Tree
0404Sum of Left Leaves
0463Island Perimeter
0530Minimum Absolute Difference in BST
0559Maximum Depth of N-ary Tree
0617Merge Two Binary Trees
0637Average of Levels in Binary Tree
0653Two Sum IV - Input is a BST
0733Flood Fill
0783Minimum Distance Between BST Nodes
0965Univalued Binary Tree
0993Cousins in Binary Tree
1379Find a Corresponding Node of a Binary Tree in a Clone of That Tree
1971Find if Path Exists in Graph

Medium (123)

Medium problems
No.Problem
0102Binary Tree Level Order Traversal
0103Binary Tree Zigzag Level Order Traversal
0107Binary Tree Level Order Traversal II
0116Populating Next Right Pointers in Each Node
0117Populating Next Right Pointers in Each Node II
0130Surrounded Regions
0133Clone Graph
0199Binary Tree Right Side View
0200Number of Islands
0207Course Schedule
0210Course Schedule II
0279Perfect Squares
0310Minimum Height Trees
0322Coin Change
0365Water and Jug Problem
0399Evaluate Division
0417Pacific Atlantic Water Flow
0429N-ary Tree Level Order Traversal
0433Minimum Genetic Mutation
0449Serialize and Deserialize BST
0513Find Bottom Left Tree Value
0515Find Largest Value in Each Tree Row
0529Minesweeper
054201 Matrix
0547Number of Provinces
0623Add One Row to Tree
0655Print Binary Tree
0662Maximum Width of Binary Tree
0672Bulb Switcher II
0684Redundant Connection
0690Employee Importance
0695Max Area of Island
0721Accounts Merge
0743Network Delay Time
0752Open the Lock
0785Is Graph Bipartite?
0787Cheapest Flights Within K Stops
0797All Paths From Source to Target
0802Find Eventual Safe States
0841Keys and Rooms
0863All Nodes Distance K in Binary Tree
0865Smallest Subtree with all the Deepest Nodes
0886Possible Bipartition
0909Snakes and Ladders
0919Complete Binary Tree Inserter
0934Shortest Bridge
0958Check Completeness of a Binary Tree
0959Regions Cut By Slashes
0967Numbers With Same Consecutive Differences
0994Rotting Oranges
1020Number of Enclaves
1034Coloring A Border
1042Flower Planting With No Adjacent
1091Shortest Path in Binary Matrix
1123Lowest Common Ancestor of Deepest Leaves
1129Shortest Path with Alternating Colors
1161Maximum Level Sum of a Binary Tree
1162As Far from Land as Possible
1202Smallest String With Swaps
1254Number of Closed Islands
1261Find Elements in a Contaminated Binary Tree
1267Count Servers that Communicate
1302Deepest Leaves Sum
1306Jump Game III
1311Get Watched Videos by Your Friends
1315Sum of Nodes with Even-Valued Grandparent
1319Number of Operations to Make Network Connected
1361Validate Binary Tree Nodes
1376Time Needed to Inform All Employees
1391Check if There is a Valid Path in a Grid
1443Minimum Time to Collect All Apples in a Tree
1448Count Good Nodes in Binary Tree
1457Pseudo-Palindromic Paths in a Binary Tree
1462Course Schedule IV
1466Reorder Routes to Make All Paths Lead to the City Zero
1519Number of Nodes in the Sub-Tree With the Same Label
1559Detect Cycles in 2D Grid
1609Even Odd Tree
1625Lexicographically Smallest String After Applying Operations
1631Path With Minimum Effort
1654Minimum Jumps to Reach Home
1765Map of Highest Peak
1905Count Sub Islands
1926Nearest Exit from Entrance in Maze
1992Find All Groups of Farmland
1993Operations on Tree
2039The Time When the Network Becomes Idle
2059Minimum Operations to Convert Number
2101Detonate the Maximum Bombs
2146K Highest Ranked Items Within a Price Range
2192All Ancestors of a Node in a Directed Acyclic Graph
2316Count Unreachable Pairs of Nodes in an Undirected Graph
2368Reachable Nodes With Restrictions
2385Amount of Time for Binary Tree to Be Infected
2415Reverse Odd Levels of Binary Tree
2467Most Profitable Path in a Tree
2471Minimum Number of Operations to Sort a Binary Tree by Level
2477Minimum Fuel Cost to Report to the Capital
2492Minimum Score of a Path Between Two Cities
2556Disconnect Path in a Binary Matrix by at Most One Flip
2583Kth Largest Sum in a Binary Tree
2596Check Knight Tour Configuration
2641Cousins in Binary Tree II
2658Maximum Number of Fish in a Grid
2685Count the Number of Complete Components
2812Find the Safest Path in a Grid
2850Minimum Moves to Spread Stones Over Grid
2998Minimum Number of Operations to Make X and Y Equal
0261Graph Valid TreePremium
0286Walls and GatesPremium
0314Binary Tree Vertical Order TraversalPremium
0323Number of Connected Components in an Undirected GraphPremium
0339Nested List Weight SumPremium
0364Nested List Weight Sum IIPremium
0490The MazePremium
0505The Maze IIPremium
0582Kill ProcessPremium
0694Number of Distinct IslandsPremium
0737Sentence Similarity IIPremium
0742Closest Leaf in a Binary TreePremium
1087Brace ExpansionPremium
1102Path With Maximum Minimum ValuePremium
1197Minimum Knight MovesPremium

Hard (58)

Hard problems
No.Problem
0126Word Ladder II
0127Word Ladder
0297Serialize and Deserialize Binary Tree
0301Remove Invalid Parentheses
0329Longest Increasing Path in a Matrix
0407Trapping Rain Water II
0488Zuma Game
0514Freedom Trail
0675Cut Off Trees for Golf Event
0685Redundant Connection II
0749Contain Virus
0765Couples Holding Hands
0773Sliding Puzzle
0778Swim in Rising Water
0815Bus Routes
0827Making A Large Island
0839Similar String Groups
0847Shortest Path Visiting All Nodes
0854K-Similar Strings
0864Shortest Path to Get All Keys
0924Minimize Malware Spread
0928Minimize Malware Spread II
0987Vertical Order Traversal of a Binary Tree
1036Escape a Large Maze
1096Brace Expansion II
1203Sort Items by Groups Respecting Dependencies
1210Minimum Moves to Reach Target with Rotations
1263Minimum Moves to Move a Box to Their Target Location
1284Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
1293Shortest Path in a Grid with Obstacles Elimination
1298Maximum Candies You Can Get from Boxes
1345Jump Game IV
1368Minimum Cost to Make at Least One Valid Path in a Grid
1377Frog Position After T Seconds
1483Kth Ancestor of a Tree Node
1568Minimum Number of Days to Disconnect Island
1970Last Day Where You Can Still Cross
2045Second Minimum Time to Reach Destination
2092Find All People With Secret
2258Escape the Spreading Fire
2290Minimum Obstacle Removal to Reach Corner
2328Number of Increasing Paths in a Grid
2360Longest Cycle in a Graph
2458Height of Binary Tree After Subtree Removal Queries
2493Divide Nodes Into the Maximum Number of Groups
2503Maximum Number of Points From Grid Queries
2577Minimum Time to Visit a Cell In a Grid
2608Shortest Cycle in a Graph
2612Minimum Reverse Operations
2617Minimum Number of Visited Cells in a Grid
2858Minimum Edge Reversals So Every Node Is Reachable
0269Alien DictionaryPremium
0302Smallest Rectangle Enclosing Black PixelsPremium
0317Shortest Distance from All BuildingsPremium
0428Serialize and Deserialize N-ary TreePremium
0431Encode N-ary Tree to Binary TreePremium
0499The Maze IIIPremium
0711Number of Distinct Islands IIPremium