18:35 What Is Recursion?
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
18:35
36:54
48:57
8:26 Recursion Educators
@gatesmashers
Welcome to Gate Smashers, one of the fastest-growing EdTech communities with 2.6 M+ learners. 🎓 We provide complete lec...
@geeksforgeeksvideos
Welcome to the Official Channel of GeekforGeeks, your one-stop destination for diverse tech education! 🚀 Tech Variety:...
@neetcode
Preparing for technical interviews? Checkout neetcode.io
@neuralnine
NeuralNine is an educational brand focusing on programming, machine learning and computer science in general! Let's deve...
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).