Skip to content
DSAPrep

    ↑↓ to move · Enter to open · Esc to close

    ← All patterns

    Backtracking

    Build candidates one choice at a time, undo the choice, try the next.

    Reach for it when

    • Generate all subsets, permutations, combinations, partitions
    • Constraint satisfaction (N-Queens, Sudoku, word search)
    • Small n (usually 20 or less)

    Core idea

    Explore a decision tree with DFS. At each level, choose an option, recurse, then undo the choice so the shared state is clean for the next option. Prune branches as early as a constraint fails; that pruning is what makes exponential searches practical.

    Template · Exponential: O(2^n) subsets, O(n!) permutations

    def subsets(nums):
    res, path = [], []
    def dfs(i):
    if i == len(nums):
    res.append(path[:]) # copy!
    return
    path.append(nums[i]) # choose
    dfs(i + 1)
    path.pop() # undo
    dfs(i + 1) # skip
    dfs(0)
    return res

    Common pitfalls

    • Appending path instead of a copy of path
    • Duplicates: sort first and skip equal siblings
    • Forgetting to undo state (visited marks, path) after recursion

    Practice · 0 / 10 done