Ordinary recursion grows the stack; optimized tail recursion can reuse it

Both compute n!. On the left, n * factorial(n-1) leaves a multiplication pending, so every frame must stay alive and the stack reaches n+1 frames. On the right, the accumulator carries the product down and the recursive call is the last action. This simulation assumes tail-call optimisation, so a single frame is reused throughout. C++ permits this transformation but does not require it. Step through and compare the two heights.

Ordinary · Θ(n) frames

peak frames: 0

Tail-recursive with TCO · Θ(1) frame

accumulator: 1