Recursion and Algorithms
Recursion and Algorithms
Recursion is a method where a function calls itself to solve a problem by breaking it down into
smaller sub-problems. It is a common technique in many algorithms, although it must be used
carefully to avoid performance issues such as stack overflow.
Key Concepts:
- Recursive Methods: Ensure a base case is defined to end recursion.
- Common Recursive Algorithms: Factorial, Fibonacci, tree traversals.
- Iterative vs. Recursive: Understanding trade-offs between recursion and loops.
Example (Factorial):
--------------------------------
public int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
--------------------------------