← capítulo

Recursion and the call stack

A function that calls itself on a smaller problem. It runs on the call stack — a stack you don't manage by hand.

Two mandatory parts

Watch Hanoi solve itself

Move n−1 aside · move the big disk · move n−1 back. Trust the recursion for the smaller problem.

Exponential cost

T(n)=2T(n1)+1=2n1T(n) = 2T(n-1) + 1 = 2^n - 1

64 disks = 2⁶⁴−1 ≈ 585 billion years. The monks are safe.

Recursion vs iteration

Takeaway

Solve the smaller problem by trusting the recursion. It's a stack in disguise. Next: divide-and-conquer sorts.