Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. It's one of the most powerful concepts in computer science.
Every recursive function needs two parts:
1. Base case — the condition that stops the recursion
2. Recursive case — the function calls itself with a smaller input
How the call stack works:
- Each function call is pushed onto the call stack
- When a base case is reached, results propagate back up
- The stack unwinds as each call returns its result
Common patterns:
- Factorial: n! = n × (n-1)!
- Fibonacci: F(n) = F(n-1) + F(n-2)
- Tree traversals: process node, then recurse on children
- Divide and conquer: split problem, solve halves, combine
Pitfalls:
- Stack overflow: too many recursive calls exhaust memory
- Redundant computation: naive recursion can be exponential
- Solution: use memoization or convert to iteration
Recursive algorithms in this visualizer:
Quick Sort, Merge Sort, DFS, N-Queens, Sudoku Solver, Tower of Hanoi
Related algorithms
Frequently asked questions
- What is Recursion?
- Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. It's one of the most powerful concepts in computer science.
- What is the complexity of Recursion?
- Recursion is explained with a step-by-step visualization, including time and space complexity where applicable.
- Who is this Recursion visualizer for?
- The Recursion visualization targets beginner-level learners in the Concepts category. Useful for students, interview prep, and hands-on review.
- What algorithms are related to Recursion?
- In the same category (Concepts) you can explore: Big O Notation, Two Pointers, Sliding Window. Each has an interactive visualization.