Recursion
A High School and College Primer for Computer Science Students
Recursion shows up on every computer science exam, in every data structures course, and in virtually every technical interview — and it confuses students far more than it should. If you've stared at a function that calls itself and had no idea how to trace what it actually does, this guide is for you.
**TLDR: Recursion** covers exactly what you need and nothing extra. You'll learn the two required ingredients of any recursive function (base case and recursive case), how to trace execution step by step using the call stack, and a repeatable recipe for writing your own recursive solutions from scratch. The guide then extends those ideas to binary trees and merge sort — the places where recursion stops being optional and becomes the natural way to think. A dedicated section on common bugs helps you diagnose missing base cases, runaway recursion, and exponential blowup before they cost you points. The book closes by connecting recursion to real applications: file systems, parsers, and AI search.
All examples are written in clean Python-style pseudocode so the logic stays front and center. Whether you're prepping for an AP Computer Science exam, working through a college intro course, or learning recursion for beginners on your own, this primer gets you oriented fast.
Pick it up, read it in one sitting, and walk into your next class or exam knowing exactly what recursion does.
- Define recursion and identify base cases and recursive cases in any recursive function
- Trace a recursive call by hand using the call stack
- Write recursive solutions for classic problems like factorial, Fibonacci, sum of a list, and reversing a string
- Recognize when recursion helps versus when iteration is the better tool
- Understand recursion on data structures, including lists, trees, and divide-and-conquer algorithms
- Spot and fix common recursion bugs: missing base cases, infinite recursion, and stack overflow
- 1. What Recursion Actually IsIntroduces recursion as a function that calls itself, with the two required ingredients (base case and recursive case) and a first worked example.
- 2. The Call Stack: How Recursion RunsShows what happens in memory when a function calls itself, using stack frames to trace execution step by step.
- 3. Writing Your Own Recursive FunctionsA repeatable recipe for designing recursive solutions, applied to sum-of-list, string reversal, and power functions.
- 4. Recursion on Structured Data: Trees and Divide-and-ConquerExtends recursion beyond numbers to data structures, covering binary trees, merge sort, and why some problems are naturally recursive.
- 5. Common Bugs and When Not to Use RecursionDiagnoses missing base cases, wrong recursive steps, and exponential blowup; compares recursion to iteration and offers guidelines for choosing.
- 6. Why Recursion MattersConnects recursion to real applications in computer science: file systems, parsers, AI search, and how thinking recursively changes problem solving.