Understanding Recursion with Recursion Trees and the Call Stack
See how recursive functions really run: base cases, recursion trees, the call stack, why naive Fibonacci is slow and how memoisation fixes it.

Understanding Recursion with Recursion Trees and the Call Stack
Recursion means solving a problem by solving smaller versions of the same problem. It produces short, elegant code, but it is hard to picture what actually happens when the program runs. The best way to understand it is to draw it. This guide explains the two pictures programmers use, the recursion tree and the call stack, and you can generate both for nine classic functions with the free recursion tree visualizer.
The two parts of every recursive function
function fact(n):
if n <= 1: return 1 # base case
return n * fact(n - 1) # recursive case
- The base case answers the smallest problem directly and stops the recursion.
- The recursive case calls the function on a smaller input and combines the result.
Forget the base case, or make the input not get smaller, and the recursion never ends until the program runs out of stack memory.
The call stack
Each call gets a stack frame holding its arguments and where to return. Calling fact(4) pushes fact(4), which pushes fact(3), then fact(2), then fact(1). fact(1) returns 1, its frame is popped, fact(2) computes 2 × 1 = 2 and returns, and so on back to fact(4) = 24.
The maximum number of frames at once is the recursion depth: 4 here. Depth matters because stack memory is limited; a depth of a million usually crashes with a stack overflow. In the visualizer the call stack panel shows exactly which calls are waiting at each step as you press Play or step through.
The recursion tree
When a function calls itself more than once, the calls branch. Each node is a call, its children are the calls it makes, and values return up the edges.
Fibonacci is the standard example:
function fib(n):
if n < 2: return n
return fib(n - 1) + fib(n - 2)
The tree for fib(5) has 15 nodes. Look closely and you will see fib(3) computed twice, fib(2) three times and fib(1) five times. The visualizer colours repeated calls red.
Why naive Fibonacci is slow
The number of calls for fib(n) is 2·fib(n + 1) − 1. It grows exponentially, roughly 1.6 times larger for each step in n. fib(20) makes 21 891 calls; fib(40) makes over 300 million.
Memoisation
The fix is to remember answers:
memo = {}
function fib(n):
if n in memo: return memo[n]
if n < 2: return n
memo[n] = fib(n - 1) + fib(n - 2)
return memo[n]
Now each value is computed once and later calls return straight from the cache. fib(5) makes 9 calls, three of them cache hits (shown in green), and fib(20) makes 39 calls instead of 21 891. The memoisation tab charts both counts for n from 1 to 20. This idea, storing answers to overlapping subproblems, is the start of dynamic programming.
Divide and conquer
Not all recursion repeats work. Merge sort splits a list in half, sorts each half recursively and merges them. Its tree has no repeated calls and a depth of about log₂ n, which is why it runs in O(n log n) time. Binary search makes only one recursive call per level, so its tree is a single path. Fast power computes xⁿ by squaring x^(n/2), needing only about log₂ n calls. The visualizer includes all three so you can compare tree shapes.
The Towers of Hanoi
Moving n discs means moving n − 1 discs out of the way, moving the largest disc, then moving the n − 1 discs back on top. That gives moves(n) = 2·moves(n − 1) + 1 = 2ⁿ − 1: 7 moves for 3 discs, 63 for 6. Seeing the tree makes the formula obvious.
Study tips
- Trace small inputs by hand, then compare with the tree.
- For any recursive function, ask: what is the base case, and does every call move towards it?
- Count calls and depth separately: calls measure time, depth measures stack memory.
Limits
The visualizer traces nine built-in functions and caps trees at 800 calls for readability. It does not run your own code, so use your language's debugger for that.


