TeachingFeatured
How I Taught Recursion in 45 Minutes
8 min
How I Taught Recursion in 45 Minutes
Recursion is one of those topics that students either "get" immediately or struggle with for weeks. Here's how I broke it down.
The Stack of Books Metaphor
I started with a simple analogy:
"Imagine you're cleaning your room and you find a stack of books. You want to know how many books are in the stack. You can only see the top book. What do you do?"
Answer: You take the top book, count it (1), then look at the next one — and repeat.
javapublic int countBooks(Stack<Book> books) { if (books.isEmpty()) return 0; // base case books.pop(); // remove top return 1 + countBooks(books); // recursive case }
The Whiteboard Flow
I drew three frames on the whiteboard:
- The call —
factorial(5)with the stack frame - The unwind — each frame returning to its caller
- The base case — highlighted in green, the "escape hatch"
javapublic static int factorial(int n) { if (n <= 1) return 1; // base case return n * factorial(n - 1); // recursive case }
Exercises I Gave
- Easy: Sum an array recursively
- Medium: Reverse a string recursively
- Hard: Tower of Hanoi
Key Takeaways
- Always start with the base case — students need to see where the recursion stops
- Draw the call stack — every single time
- Use real-world metaphors — Russian dolls, mirrors facing each other, nested Matryoshka
Recursion is not magic. It's just a function that calls itself with a smaller problem. Trust the stack.