Algorithmsrecursion stack manipulation

Recursion and the Call Stack

TT
Testlaa Team
May 14, 20261 min read

Each recursive call is a stack frame: locals and return address. Understanding depth explains stack overflow and tail-call intuition.

Try this in Python

def fact(n: int) -> int:
    if n <= 1:
        return 1
    return n * fact(n - 1)


print(fact(5))

Key takeaways

  • Depth equals maximum simultaneous frames.
  • Iterative versions often use an explicit stack to mimic the recursion tree.
  • Know your language’s recursion limit for contests.

Tags:

Stacks & queuesPythonStudents