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
# | Problem | Link | Concept | Description of Difficulty Increase |
---|---|---|---|---|
1 | Maximum Depth of Binary Tree | Link | Level-Order Traversal (BFS) | Base case. Introduces the core BFS pattern of using a queue to simply count the levels of a tree. |
2 | Minimum Depth of Binary Tree | Link | BFS with Early Exit | Builds on max depth by adding anearly exit condition. You must stop the search as soon as the first leaf node is found. |
3 | Univalued Binary Tree | Link | BFS with State Comparison | Introduces state. You must store the root’s value and compare every subsequent node against it, exiting early on a mismatch. |
4 | Invert Binary Tree | Link | BFS with Node Modification | Introduces modifying the tree structure during traversal. Each node’s children are swapped as it’s processed. |
Group 2: Simultaneous Traversal & Structural Comparison
# | Problem | Link | Concept | Description of Difficulty Increase |
---|---|---|---|---|
5 | Same Tree | Link | Synchronized BFS | Moves from one tree to two. Requires traversing two trees simultaneously and comparing corresponding nodes at each step. |
6 | Symmetric Tree | Link | Synchronized 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 ). |
7 | Merge Two Binary Trees | Link | Synchronized BFS with Modification | Builds 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
# | Problem | Link | Concept | Description of Difficulty Increase |
---|---|---|---|---|
8 | Average of Levels in Binary Tree | Link | Level-by-Level Aggregation | The canonical level-order problem. Requires explicitly processing nodes one level at a time to perform an aggregation (sum and count). |
9 | Sum of Left Leaves | Link | BFS with Child-Node Inspection | Increases complexity by requiring you to inspect a node’s children (to check if they are leaves) rather than just the node itself. |
10 | Cousins in Binary Tree | Link | BFS 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. |
11 | Maximum Depth of N-ary Tree | Link | BFS Generalization | Boring 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
# | Problem | Link | Concept | Description of Difficulty Increase |
---|---|---|---|---|
12 | Flood Fill | Link | Grid 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. |
13 | Island Perimeter | Link | Grid Traversal with Logic | While 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. |
14 | Find if Path Exists in Graph | Link | Abstract 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
# | Problem | Link | Concept | Description of Difficulty Increase |
---|---|---|---|---|
15 | Two Sum IV - Input is a BST | Link | BFS + Hash Set | Combines BFS traversal with another data structure. You traverse the tree and use a hash set for efficient lookups to find the sum target. |
16 | Minimum Absolute Difference in BST | Link | BFS + 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. |
17 | Minimum Distance Between BST Nodes | Link | BFS + Post-processing (Suboptimal) | Boring variation. This is the exact same problem as the one above, just rephrased. The solution and learning point are identical. |