Recursion trees
28 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)When a method makes one recursive call per step (like factorial(n)), its execution looks like a straight chain. When a method makes two or more recursive calls per step (like fib(n - 1) + fib(n - 2)), its calls branch out into a recursion tree—and drawing that tree makes both time complexity and stack space obvious at a glance.
Drawing calls as a tree
A recursion tree is a drawing of every call a recursive method makes, with each call as a node; it shows how much work the recursion does.
In a recursion tree, each node represents a single method call (labeled with its argument, such as fib(4)), and its children are the direct recursive calls it makes (fib(3) and fib(2)).
Example 1 · Printing the full recursion tree of fib(4) with indentation
public class FibTreePrinterDemo { public static int fibTree(int n, int depth) { System.out.println(" ".repeat(depth) + "fib(" + n + ")"); if (n <= 1) { return n; } return fibTree(n - 1, depth + 1) + fibTree(n - 2, depth + 1); }
public static void main(String[] args) { int ans = fibTree(4, 0); System.out.println("Result: " + ans); }}fib(4)
fib(3)
fib(2)
fib(1)
fib(0)
fib(1)
fib(2)
fib(1)
fib(0)
Result: 3What this code does
- Count the printed fib(...) lines: there are 9 nodes in the recursion tree of fib(4).
- Notice that the indentation never exceeds 3 levels (depth 0, 1, 2, 3 -> 4 frames on the stack), even though 9 calls run in total.
The leaves at the bottom of the tree are the base-case calls that make no further calls. How does the JVM actually walk this tree at runtime? It does not run all nodes on a level in parallel! Instead, Java evaluates fib(n - 1) + fib(n - 2) from left to right, which means the call stack walks the recursion tree in depth-first (pre-order) order:
- It dives all the way down the leftmost branch (
fib(4) -> fib(3) -> fib(2) -> fib(1)). - It pops
fib(1), runs the right childfib(0)offib(2), popsfib(2), and only then explores the right childfib(1)offib(3).
- Root node: the initial top-level call (e.g.,
fib(4)). - Internal nodes: recursive calls that spawn child calls.
- Leaf nodes: base cases (
fib(1)andfib(0)) that return immediately.
Counting nodes to find the cost
A recursion tree gives you two numbers that beginners often confuse—total nodes vs tree height—and they measure two completely different costs.
Example 2 · Measuring total nodes (time) vs max depth (space) for fib(n)
public class NodesVsHeightDemo { static int totalCalls; static int maxDepth;
public static int fib(int n, int depth) { totalCalls++; maxDepth = Math.max(maxDepth, depth); if (n <= 1) return n; return fib(n - 1, depth + 1) + fib(n - 2, depth + 1); }
public static void main(String[] args) { for (int n : new int[]{4, 6, 10, 20}) { totalCalls = 0; maxDepth = 0; int val = fib(n, 1); System.out.println("n=" + n + ": fib=" + val + ", totalCalls=" + totalCalls + ", maxStackDepth=" + maxDepth); } }}n=4: fib=3, totalCalls=9, maxStackDepth=4 n=6: fib=8, totalCalls=25, maxStackDepth=6 n=10: fib=55, totalCalls=177, maxStackDepth=10 n=20: fib=6765, totalCalls=21891, maxStackDepth=20
What this code does
- As n doubles from 10 to 20, maxStackDepth merely doubles from 10 to 20 (O(n) space).
- Meanwhile, totalCalls explodes from 177 to 21,891 (exponential time).
Example 3 · Binary recursion tree where n halves: 2 calls of n / 2 has only 2n - 1 nodes
public class HalvingTreeNodesDemo { static int calls;
// Makes 2 calls of n / 2 instead of n - 1 public static void binaryHalving(int n) { calls++; if (n <= 1) return; binaryHalving(n / 2); binaryHalving(n / 2); }
public static void main(String[] args) { for (int n : new int[]{4, 8, 16, 32}) { calls = 0; binaryHalving(n); System.out.println("n=" + n + " -> total nodes=" + calls); } }}n=4 -> total nodes=7 n=8 -> total nodes=15 n=16 -> total nodes=31 n=32 -> total nodes=63
What this code does
- Why does making 2 calls of n / 2 give O(n) nodes (2n - 1) instead of O(2^n)? Because the tree height is only log2(n)!
- A binary tree of height log2(n) has 1 + 2 + 4 + ... + n = 2n - 1 nodes.
In detail:
- Time complexity = (number of nodes in the recursion tree) × (work done per node).
- Call-stack space complexity = height of the recursion tree (the longest root-to-leaf path), because sibling branches reuse the same stack memory after the left branch finishes and pops!
Suppose each call does O(1) work outside its recursive calls and makes 2 recursive calls of size n - 1:
- Level 0 (root): 1 = 2⁰ node.
- Level 1: 2 = 2¹ nodes.
- Level 2: 4 = 2² nodes.
- Level d: 2ᵈ nodes. Summing across all levels from 0 to n gives 1 + 2 + 4 + … + 2ⁿ = 2ⁿ⁺¹ - 1 = O(2ⁿ) total nodes (time complexity), while the call stack only holds one root-to-leaf path of length n + 1 at any instant: O(n) stack space!
Predict the output
Count the nodes of the tree
Every call is one node. Pick what each program prints.
Round 1 of 2
Each call makes two calls of n - 1. How many calls for f(2)?
fib(n) uses O(2ⁿ) memory! It takes O(2ⁿ) time, but only O(n) stack space because at most n + 1 frames ever sit on the call stack at the same moment.The Fibonacci tree
Let us trace the recursion tree of fib(4) where fib(0) = 0, fib(1) = 1, and fib(n) = fib(n - 1) + fib(n - 2):
- Root
fib(4)callsfib(3)andfib(2)(2 calls). fib(3)callsfib(2)andfib(1)(2 calls).- Each
fib(2)callsfib(1)andfib(0)(4 calls).
In total, fib(4) executes 9 method calls (1 + 2 + 4 + 2 = 9). Even more telling is a useful exact identity: the total number of calls made by fib(n) is always 2 · F[n+1] - 1! Look how fast that grows:
fib(4): F₅ = 5 ⇒ 2(5) - 1 = 9 calls, max stack depth = 4.fib(6): F₇ = 13 ⇒ 2(13) - 1 = 25 calls, max stack depth = 6.fib(10): F₁₁ = 89 ⇒ 2(89) - 1 = 177 calls, max stack depth = 10.fib(40): F₄₁ = 165,580,141 ⇒ 331,160,281 calls, max stack depth = 40!
Recursion Tree of fib(4) (9 Calls, Max Stack Depth 4)
Step 1 of 5
- calls
- 4
- depth
- 4
- fib(4)
- fib(3)
- fib(2)
- fib(1)
Next step
Check your understanding
10 quick questions