โ† Competitive Programming learning path
Competitive Programming ยท Module 7 of 10

Recursion and backtracking

Explore choices step by step and undo them.

๐Ÿ“– Key ideas

  1. Recursion solves a problem through smaller copies of itself.
  2. Always define a base case to stop.
  3. Backtracking tries a choice, recurses, then undoes it.
  4. Generate subsets, permutations and N-Queens with backtracking.
  5. Prune branches that cannot lead to a valid answer.
๐Ÿ’ก Real-life example: Backtracking is like walking a maze: at a dead end, step back to the last junction and try another path.
Base caseCondition that stops recursion
BacktrackingTry, recurse, undo
PruningSkip hopeless branches
PermutationAn ordering of items