Two recursive calls, one tree
The Fibonacci sequence is 0, 1, 1, 2, 3, 5, …. Recursively: if n ≤ 1, return n; otherwise return fib(n − 1) + fib(n − 2).
Unlike factorial’s single chain, each call branches. Watch the tree fill in — then notice how many orange nodes redo work that was already finished elsewhere.
fib(4). Then try fib(5) and count how many times fib(2) appears.def fib(n): if n <= 1: return n return fib(n - 1) + fib(n - 2)
This version is correct but not efficient — the tree proves why.
Run the simulation
Nodes stay on the tree after they return so you can compare identical subproblems side by side.
Why the naive tree wastes work
Correct answers, exponential cost — the overlapping subproblems are the lesson.
Two calls per step
Each non-base call opens fib(n−1) and fib(n−2), so the tree roughly doubles each level.
Same fib(k) again
Orange nodes redo a value that already finished elsewhere. Nothing is remembered between branches.
Memoize or iterate
Store answers (memoization / DP) or build upward in a loop so each n is solved once.