account_tree

Recursion & Backtracking
Curriculum

A structured path to mastering recursive problem decomposition and state-space exploration through decision trees.

What This Pattern Is Really About

call_splitState Decomposition

Breaking problems into smaller identical subproblems. Each recursive call operates on a reduced input with the same structure.

device_hubDecision Trees

Explicit branching structure of choices. Each node represents a decision point; edges represent choices; leaves are solutions or dead ends.

undoBacktracking

Systematic exploration with state restoration. Make choice, recurse, undo choice. Guarantees all paths explored without state leakage.

flagBase Cases

Termination conditions that halt recursion. Define when problem is trivially solvable without further decomposition.

Mental Model & Invariant

Recursion is structured problem reduction with explicit state contracts.

"At each recursive call, the problem structure remains identical but the input size decreases. State is either preserved (immutable) or explicitly restored (backtracking)."

The Core Invariant
Every recursive call makes measurable progress toward a base case. The call stack contract defines what each level receives and returns.

Break this, and you get infinite recursion or incorrect results.

verified

Call Stack Contract

Define parameters, return type, and what each call guarantees. Every level must honor this contract.

gavel

State Invariant

What remains true across all recursive levels. Immutable state is passed down; mutable state must be restored after exploration.

flag

Base Case First

Always define termination conditions before recursive cases. Base cases are non-negotiable—they prevent infinite recursion.

When to Think "Recursion & Backtracking"

sensorsStrong Signals

draw

Generate all combinations/permutations

draw

Explore all paths in tree/graph

draw

Divide-and-conquer structure

draw

Choice at each step with constraints

draw

Subproblems have identical structure

draw

Solution requires exhaustive search

ruleConstraint Signals

lock_clock

Must explore entire solution space

lock_clock

Decisions are reversible

lock_clock

Problem decomposes recursively

lock_clock

No greedy solution exists

Variants You Must Master

function

Pure Recursion

No state modification. Return values only. Immutable parameters.

// Invariant
Each call returns result; no side effects.
Tree traversalFactorialFibonacci
undo

Backtracking with State

Modify state, explore, restore. Explicit undo after recursion.

// Invariant
State restored after each branch explored.
N-QueensSudokuPermutations
storage

Memoization (Top-Down DP)

Cache subproblem results. Avoid recomputation.

// Invariant
Each subproblem computed once.
FibonacciCoin changeWord break
call_split

Divide and Conquer

Split problem, recurse on parts, combine results.

// Invariant
Subproblems independent; combine step merges.
Merge sortQuick sortBinary search

How to Learn This Pattern

1

Draw the Decision Tree

Sketch the tree explicitly on paper. Each node is a state; edges are choices. Identify leaves (base cases) and internal nodes (recursive cases).

2

Define Recursive Signature

Write function signature: parameters (what changes), return type (what you compute). This is your call stack contract.

3

Identify Base Cases

When is the problem trivially solvable? When do you stop recursing? Write these conditions first—before any recursive logic.

4

Define Recursive Case

How do you decompose the problem? What choices exist? How do you combine results from recursive calls?

5

Verify State Management

Is state immutable (pass copies) or mutable (restore after recursion)? Trace what happens to state across calls.

6

Trace Execution

Walk through a small input by hand. Draw the call stack. Verify base cases hit, state is correct, and results combine properly.

Canonical Problem List

Common Mistakes

error_outline

Missing or Incorrect Base Cases

Failure Mode

Infinite recursion or stack overflow. Function never terminates.

Mastery Fix

Write base cases first. Verify every recursive path eventually hits a base case.

error_outline

Not Restoring State After Backtracking

Failure Mode

State leaks across branches. Subsequent explorations see modified state from previous branches.

Mastery Fix

Explicitly undo every state modification after recursive call returns. Use add/recurse/remove pattern.

error_outline

Passing Mutable State Without Copying

Failure Mode

All recursive calls share same state object. Modifications affect other branches.

Mastery Fix

Pass copies of mutable state or use immutable data structures. Verify each call has independent state.

error_outline

No Progress Toward Base Case

Failure Mode

Infinite recursion. Input size doesn't decrease or termination condition never met.

Mastery Fix

Verify each recursive call moves closer to base case. Check that parameters change meaningfully.

error_outline

Confusing Recursion with Iteration

Failure Mode

Trying to think iteratively about recursive problems. Misses the decomposition structure.

Mastery Fix

Focus on: what does this call do? What do recursive calls return? How do I combine results?

Interview Guidance

Interview Protocol

What they test

  • Base Case Identification
  • State Management Precision
  • Decision Tree Articulation

Strategy

Avoid "I'll use recursion" without structure. Instead: "I'll explore all choices at each step. Each recursive call handles a smaller subproblem. Base case is when no more choices remain."

The Verbal Template

Base Case

"When [condition], the problem is trivially solved. I return [value]."

Choices

"At this step, I can choose [option A] or [option B]. Each choice leads to a recursive call."

Recursion

"I make a choice, recurse on the smaller problem, then [combine/backtrack]."

State

"I [pass immutable copy / modify and restore] state to ensure branches don't interfere."

When You've Mastered It

done_all

Draw decision tree immediately

done_all

Base cases defined before recursion

done_all

State management explicit and correct

done_all

Call stack contract articulated clearly

done_all

Backtracking restore logic automatic

done_all

Trace execution without debugger

What This Unlocks Next

account_tree

Dynamic Programming

Recursion with memoization. Cache subproblem results to avoid recomputation.

hub

Graph Traversal

DFS is recursive backtracking on graphs. BFS uses explicit queue instead of call stack.

call_split

Divide and Conquer

Recursive decomposition with independent subproblems and combine step.

psychology

State-Space Search

Systematic exploration of solution spaces with pruning and optimization.