Compile Ready
Module 1 · Backtracking Fundamentals

The Recursion Tree

The recursion tree shows the actual calls made by the algorithm, making it the fastest way to explain traversal order, pruning, and complexity.

7 min readConcept
BacktrackingRecursion TreeComplexity

How to Draw It

Start with the initial call as the root. Label each node with the state that matters, not every local variable. For subsets, use the current index and path. For permutations, use the prefix and remaining unused values. For a board, use the row or cell being filled plus the placements already made.

Then draw children by applying each legal next choice. Stop at base cases and explicitly mark whether the leaf records an answer, returns false, or gets pruned. A small input is enough; the goal is insight, not a complete picture for large n.

Counting Leaves

Leaves often correspond to complete candidates. Subset recursion with include or exclude has 2^n leaves because every element has two outcomes. Permutation recursion has n! leaves because the first position has n choices, the second has n - 1, and so on.

Counting leaves gives a lower bound on time when the problem must output all solutions. If there are 2^n subsets, no algorithm can list them in less than O(2^n) output steps.

Counting Internal Nodes

Time is not only leaves. Every internal node also does work: checking constraints, looping over choices, marking state, and undoing state. For many backtracking families, the number of internal nodes is within the same exponential order as the leaves, so the leaf count gives the right growth class.

When branching factor varies, use an upper bound. If every level has at most b choices and depth at most d, the tree has at most 1 + b + b^2 + ... + b^d nodes, which is O(b^d) for b > 1.

Reading Pruning From the Tree

A pruned branch is a node whose children are never drawn. Marking those cuts on the recursion tree helps interviewers see exactly what your optimization saves. It also protects correctness because you can explain why the omitted subtree cannot contain a useful answer.

For duplicate pruning, the tree shows that two sibling edges would produce the same set of descendants. For bound pruning, it shows that a partial state lacks enough remaining choices or has already exceeded a target.

Subset recursion for [1, 2]

  • index 0, path []
    • choose 1
      • choose 2record [1, 2]
      • skip 2record [1]
    • skip 1
      • choose 2record [2]
      • skip 2record []

Each root-to-leaf path decides one outcome for each element. The same drawing style scales to permutations, combinations, strings, and boards.

Counting nodes in a binary recursion tree

Loading…

The return value counts one node for the current call plus the nodes in both children, matching the drawn recursion tree.

Key Takeaways

  • A recursion tree labels the calls actually made by the algorithm, not just the final outputs.
  • Leaves often count complete candidates, while internal nodes account for loop and constraint work.
  • A branching factor **b** and depth **d** give the common upper bound **O(b^d)**.
  • Marking pruned nodes makes performance improvements and correctness arguments visible.