Recursion tree
The drawing of all the calls of a recursive function that calls itself more than once: each call is a node and its children hang from it.
Example
In Fibonacci, f(5) branches into f(4) and f(3), f(4) into f(3) and f(2)... f(3) shows up twice and f(2), 3 times.
Test yourself: Recursion tree
Iteration and recursion
In recursive Fibonacci, f(n) = f(n − 1) + f(n − 2), what happens with each call?
f(5) calls f(4) and f(3); f(4) calls f(3) and f(2)... The calls double at each level and form a tree.
Tower of Hanoi
Each call with i disks opens ___ calls with i − 1.
One to take the i − 1 to the helper and another to bring them to the target: with 3 disks, 7 calls in total.
Dynamic programming
Computing dp[n] with recursion and storing nothing costs O(___).
Each call opens 2: with n = 40 that is hundreds of millions of calls.
0/3
▶ Play