Recursion is when a function calls itself to solve a problem by breaking it into smaller subproblems. Every recursive function has a base case (when to stop) and a recursive case (how to break down the problem). It naturally solves problems with self-similar structure: trees, fractals, nested data.

How Recursion Works

Factorial: factorial(5) = 5 * factorial(4) = 5 * 4 * factorial(3) ... until factorial(1) = 1 (base case). File system traversal: to list all files, list files in the current directory, then recursively list files in each subdirectory.

Recursive thinking: if you can solve the problem for n-1, can you use that to solve it for n? That's recursion. Tree traversal, merge sort, quicksort, and many graph algorithms are naturally recursive.

Key Concepts

  • Base Case — The condition that stops recursion — without it, you get infinite recursion and stack overflow
  • Recursive Case — The part that breaks the problem down and calls the function again with a smaller input
  • Call Stack — Each recursive call adds a frame to the stack — too many calls cause stack overflow
  • Tail Recursion — Recursion where the recursive call is the last operation — some languages optimize this to avoid stack overflow

Learn Recursion — Top Videos

Recursion Educators

Gate Smashers
Gate Smashers

@gatesmashers

Data Science

Welcome to Gate Smashers, one of the fastest-growing EdTech communities with 2.6 M+ learners. 🎓 We provide complete lec...

2.6M Subs
2K Videos
13.8K Avg Views
1.58% Engagement
View Profile →
NeetCode
NeetCode

@neetcode

CS

Current NEET and ex-Google SWE, also I love teaching! N.E.E.T. = (Not in education, employment or training) Preparing ...

1.1M Subs
414 Videos
128.4K Avg Views
3.99% Engagement
View Profile →
Greg Hogg
Greg Hogg

@greghogg

Data Science

Today, Greg is driven by a single mission: to help engineers master the complex technical skills required to land roles ...

309K Subs
1.3K Videos
9.1K Avg Views
3.31% Engagement
View Profile →

Frequently Asked Questions

Recursion vs iteration?

Any recursive solution can be written iteratively (using loops and a stack). Recursion is more elegant for tree/graph problems. Iteration is usually faster and uses less memory. Use whichever is clearer.

How do I think recursively?

Ask: what's the simplest case I can solve directly? (base case). Can I express the solution in terms of a smaller version of the same problem? (recursive case).

Want a structured learning path?

Plan a Recursion Lesson →