Recursion is an approach to problem solving where the solution depends partially on solutions to smaller instances of related problems.
A recursive function consists of base cases and calls to the same function with different arguments. Two key ingredients to a successful use of recursion are identifying the base cases, which are to be solved directly, and ensuring progress, that is the recursion converges to the solution.
-
What is the base case ? i.e. A stopping condition that allows you stop a recursive program or function
-
What is the least amount of work I can do in each Iteration (function call)
- Bridges the gap between elegance and complexity
- Reduces the need for complex loops and auxilliary data structures
- Can reduce time easily with memoization
- Works really well with recursive data structures like trees & Graphs
- Slowness due to CPU Overload
- Can lead to out of memory errors / Stack overflow exceptions
- Can be unnecessarily complex if poorly constructed
In this section we will solve common recursion questions that will help you understand the basics of recursion