Data Structures & Algorithms · Revision notes

Recursion & Backtracking

Recursion solves a problem by reducing it to smaller instances of itself with a base case. Backtracking builds candidates incrementally, abandons (prunes) branches that cannot succeed, and undoes choices to explore alternatives.

Quick revision

  • Base case + progress + combine; depth = stack space
  • Naive Fibonacci O(phi^n); memoize -> O(n); Hanoi 2^n - 1
  • Backtrack: choose, explore, unchoose; copy path; prune
  • Duplicates: sort, skip a[i]==a[i-1] when i > start
  • N-Queens: cols, r-c, r+c sets; N=8 -> 92 solutions
Read the full Recursion & Backtracking notes on Doubtclip →