Patterns
B · Pattern 13
Backtracking
Explore all possibilities via recursion, but prune branches that cannot lead to valid solutions. Build the solution incrementally and undo choices when they lead to dead ends.
- Time
- O(n!) worst case
- Space
- O(n) recursion depth
Spot it when
- Generate all valid combinations/permutations
- Constraint satisfaction (Sudoku, N-Queens)
- Word search in grid
- "Find all solutions" problems