DSA Notes
Solve 12 classic recursion problems — Tower of Hanoi, string permutations, power set, balanced parentheses, palindrome check, climbing stairs, flood fill, binary search, merge sort, tree problems, and staircase with k steps.
Problem 1 — Tower of Hanoi
Problem: Move n disks from source peg to destination peg using auxiliary peg. Rules: only one disk at a time, never place larger disk on smaller.
def hanoi(n, source, destination, auxiliary):
"""
Move n disks from source to destination using auxiliary.
Time: O(2ⁿ), Space: O(n)
"""
if n == 1:
print(f"Move disk 1 from {source} to {destination}")
return
# Move top n-1 disks from source to auxiliary (using destination)
hanoi(n-1, source, auxiliary, destination)
# Move largest disk from source to destination
print(f"Move disk {n} from {source} to {destination}")
# Move n-1 disks from auxiliary to destination (using source)
hanoi(n-1, auxiliary, destination, source)
hanoi(3, 'A', 'C', 'B')
# Move disk 1 from A to C
# Move disk 2 from A to B
# Move disk 1 from C to B
# Move disk 3 from A to C
# Move disk 1 from B to A
# Move disk 2 from B to C
# Move disk 1 from A to CTotal moves = 2ⁿ - 1
Problem 3 — Power Set (All Subsets)
Problem 4 — Generate Valid Parentheses
Problem 5 — Check Palindrome Using Recursion
Problem 6 — Climbing Stairs (k steps at a time)
def climb_stairs(n, k=2):
"""
Count ways to climb n stairs, taking 1 to k steps at a time.
With k=2: standard problem. Time: O(kⁿ) naive, O(kn) with memo.
"""
from functools import lru_cache
@lru_cache(maxsize=None)
def dp(remaining):
if remaining == 0: return 1 # One way: take no step
if remaining < 0: return 0 # Impossible
return sum(dp(remaining - i) for i in range(1, k + 1))
return dp(n)
print(climb_stairs(5, 2)) # 8 ways with max 2 steps
print(climb_stairs(4, 3)) # 7 ways with max 3 stepsProblem 7 — Flood Fill
Problem 8 — Binary Search (Recursive)
Problem 9 — Merge Sort
Problem 10 — Tree: Maximum Path Sum
Problem 11 — Flatten Nested List
Problem 12 — Count Ways to Reach Score
Complexity Summary
| Problem | Time | Space | Key Technique |
|---|---|---|---|
| Tower of Hanoi | O(2ⁿ) | O(n) | Reduce-to-subproblem |
| Permutations | O(n!×n) | O(n!) | Choose + recurse on rest |
| Power Set | O(2ⁿ×n) | O(2ⁿ) | Include/exclude |
| Valid Parentheses | O(2ⁿ/√n) | O(n) | Constrained backtracking |
| Palindrome Check | O(n) | O(n) | Two pointers inward |
| Climbing Stairs | O(kn) | O(n) | Memoized recursion |
| Flood Fill | O(m×n) | O(m×n) | 4-directional DFS |
| Binary Search | O(log n) | O(log n) | Halving |
| Merge Sort | O(n log n) | O(n) | Divide-and-conquer |
| Max Path Sum | O(n) | O(h) | Post-order DFS |
| Flatten List | O(n) | O(depth) | Recursive flatten |
| Count Ways | O(n×k) | O(n) | Memoized DP |
*Next Lesson: Backtracking Introduction — Using recursion with "undo" to explore all possibilities.*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Recursion Problems — 12 Classic Problems from Basic to Advanced.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, recursion, problems, recursion problems — 12 classic problems from basic to advanced
Related Data Structures & Algorithms Topics