5 min read
BFS patterns in LeetCode Problems (Easy)

The problems are grouped into logical themes: starting with fundamental tree traversals, moving to more complex state tracking and comparisons, and finally generalizing the concept to graphs and grids. This progression is designed to build your skills incrementally.

Group 1: Foundational Tree Traversal & Properties

#ProblemLinkConceptDescription of Difficulty Increase
1Maximum Depth of Binary TreeLinkLevel-Order Traversal (BFS)Base case. Introduces the core BFS pattern of using a queue to simply count the levels of a tree.
2Minimum Depth of Binary TreeLinkBFS with Early ExitBuilds on max depth by adding anearly exit condition. You must stop the search as soon as the first leaf node is found.
3Univalued Binary TreeLinkBFS with State ComparisonIntroduces state. You must store the root’s value and compare every subsequent node against it, exiting early on a mismatch.
4Invert Binary TreeLinkBFS with Node ModificationIntroduces modifying the tree structure during traversal. Each node’s children are swapped as it’s processed.

Group 2: Simultaneous Traversal & Structural Comparison

#ProblemLinkConceptDescription of Difficulty Increase
5Same TreeLinkSynchronized BFSMoves from one tree to two. Requires traversing two trees simultaneously and comparing corresponding nodes at each step.
6Symmetric TreeLinkSynchronized BFS (Self-Comparison)Boring variation of “Same Tree”. Applies the same-tree logic to a single tree’s subtrees, requiring a mirrored comparison (left.left vs right.right).
7Merge Two Binary TreesLinkSynchronized BFS with ModificationBuilds on “Same Tree” by adding modification logic (summing values) and handling structural asymmetries (when one tree has a node but the other doesn’t).

Group 3: Level-by-Level Processing & Advanced State Management

#ProblemLinkConceptDescription of Difficulty Increase
8Average of Levels in Binary TreeLinkLevel-by-Level AggregationThe canonical level-order problem. Requires explicitly processing nodes one level at a time to perform an aggregation (sum and count).
9Sum of Left LeavesLinkBFS with Child-Node InspectionIncreases complexity by requiring you to inspect a node’s children (to check if they are leaves) rather than just the node itself.
10Cousins in Binary TreeLinkBFS with State Tracking (Parent & Depth)Requires tracking additional state for each node in the queue (its parent and depth) to satisfy the problem’s conditions.
11Maximum Depth of N-ary TreeLinkBFS GeneralizationBoring variation of “Maximum Depth of Binary Tree”. Reinforces the core BFS concept by generalizing it from fixed children (left, right) to a list of children.

Group 4: BFS on Grids and Graphs

#ProblemLinkConceptDescription of Difficulty Increase
12Flood FillLinkGrid Traversal (BFS)Major conceptual leap. Applies BFS to a 2D grid instead of a tree, introducing 4-directional traversal and the need for a visited set to avoid cycles.
13Island PerimeterLinkGrid Traversal with LogicWhile not a pure BFS problem (a simple iteration is more optimal), it builds on grid traversal. The challenge is in the logic of counting exposed sides, not the traversal itself.
14Find if Path Exists in GraphLinkAbstract Graph Traversal (BFS)Generalizes grid traversal to an abstract graph. Requires building an adjacency list from edges before applying the standard BFS algorithm.

Group 5: Combining BFS with Other Data Structures

#ProblemLinkConceptDescription of Difficulty Increase
15Two Sum IV - Input is a BSTLinkBFS + Hash SetCombines BFS traversal with another data structure. You traverse the tree and use a hash set for efficient lookups to find the sum target.
16Minimum Absolute Difference in BSTLinkBFS + Post-processing (Suboptimal)A multi-step problem if using BFS. Requires traversing, storing all values, sorting them, and then finding the minimum difference. Highlights when another approach (in-order DFS) is superior.
17Minimum Distance Between BST NodesLinkBFS + Post-processing (Suboptimal)Boring variation. This is the exact same problem as the one above, just rephrased. The solution and learning point are identical.