Backtracking
Tactic
Backtracking explores a decision tree one partial candidate at a time. It chooses an option, recurses, then undoes the choice before trying the next option.
The invariant is that the current partial candidate is valid under the rules checked so far. Pruning cuts off branches as soon as they cannot lead to a complete answer.
The undo step matters. Mutating a shared list, board, or set is efficient, but every mutation needs a matching cleanup. Copying state is simpler but can add large hidden costs.
Value
The value is controlled exhaustive search. Backtracking does not magically remove exponential complexity, but it makes exponential search readable and prunable.
Direct complexity example
- Brute force: Generate every raw candidate and validate only at the end: often time, where many branches are doomed early.
- With this tactic: Validate and prune during construction: worst-case still exponential, but practical work can drop sharply.
- Space: Space is recursion depth plus the current candidate, not counting the output. Copy-heavy versions can use much more.
Challenges this solves
- subsets and combinations
- permutations
- N-Queens
- word search
- constraint puzzles
- combination sum
When to use it
Use this tactic when these conditions are true:
- the problem asks for all valid arrangements or choices
- each choice affects the remaining choices
- invalid partial candidates can be detected early
- the constraints are small enough for exponential search
When not to use it
Reach for a different tactic when these warning signs appear:
- only one optimum is needed and DP or greedy can summarize the search
- the branching factor and depth make exhaustive search impossible
- there are repeated states that should be memoized
- the problem has no natural partial validity check
Terminology clues
These prompt words often point toward this concept:
- all combinations
- all permutations
- place
- choose
- valid board
- search
- undo
- prune
Problems that use it
- 17. Letter Combinations of a Phone Number
- 22. Generate Parentheses
- 39. Combination Sum
- 40. Combination Sum II
- 46. Permutations
- 51. N-Queens
- 79. Word Search
- 90. Subsets II
- 131. Palindrome Partitioning
- 212. Word Search II