Recursion & Backtracking
The Mental Model
Recursion is delegation. Instead of solving the whole problem, you solve a smaller version and combine results. Backtracking is systematic trial and error—try a choice, explore all consequences, undo the choice, try the next.Pattern Recognition Signals:
- “Generate all combinations/permutations”
- “Find all valid configurations”
- Problem has natural substructure (solve for n-1, then n)
- Constraints are small (n ≤ 20 for backtracking, n ≤ 10 for permutations)
When to Use
Recursion
- Tree/graph traversals
- Divide and conquer
- Problems with substructure
- DP (memoized recursion)
Backtracking
- Generate combinations/subsets
- Permutations
- Constraint satisfaction (Sudoku, N-Queens)
- Path finding with constraints
The Recursion Framework
Every recursive function has three parts:Pattern 1: Subsets (Power Set)
Problem: Generate all 2ⁿ subsets of an array. Approach: For each element, make two choices: include it or don’t.| Problem | Rating | Link |
|---|---|---|
| Good Sequences | 1500 | CF 264B |
Pattern 2: Permutations
Problem: Generate all n! permutations. Approach: Fix each element at the current position, recurse for the rest.Pattern 3: Combinations
Problem: Generate all C(n, k) combinations of size k. Key Insight: Like subsets, but only output when we have exactly k elements.Pattern 4: Constraint Satisfaction
Problem: N-Queens - Place N queens on NxN board so none attack each other. Approach: Place queens row by row, check constraints before placing.Pattern 5: Path Finding with Backtracking
Problem: Find all paths from start to end in a graph/grid.The Backtracking Template
Pruning: The Key to Efficiency
Backtracking can be slow (exponential). Pruning cuts branches early.Types of Pruning
- Constraint Pruning: Skip choices that violate constraints
- Bound Pruning: Skip if current path can’t lead to better solution
- Symmetry Pruning: Skip symmetric configurations
Common Mistakes
Recursion vs Iteration
| Aspect | Recursion | Iteration |
|---|---|---|
| Readability | Often cleaner | Can be verbose |
| Space | O(depth) stack | O(1) usually |
| Speed | Function call overhead | Slightly faster |
| Stack overflow risk | Yes (depth > 10⁴) | No |
Practice Problems
Beginner (800-1100)
| Problem | Concept | Link |
|---|---|---|
| Subsets | Basic backtracking | LeetCode 78 |
| Fibonacci | Basic recursion | Classic |
Intermediate (1100-1400)
Advanced (1400-1700)
| Problem | Concept | Link |
|---|---|---|
| N-Queens | Constraint satisfaction | CSES |
| Grid Paths | Path counting | CSES |
| Word Search | Backtracking on grid | LeetCode 79 |
Key Takeaways
Three Parts
Every recursion: base case, recursive case, combine results.
Backtrack = Undo
After recursing, restore state to explore other branches.
Prune Early
Skip invalid branches as soon as possible to avoid TLE.
Watch the Stack
Recursion depth > 10⁴ risks stack overflow on most judges.
Next Up
Chapter 9: Greedy Algorithms
Learn when local optimal choices lead to global optimal solutions—and when they don’t.