Chapter 13: Recursion
What is Recursion?
- Recursion occurs when a method calls itself or makes a self-reference.
- It also occurs when several methods form a calling cycle.
For example, method A calls method B, method B calls method C,
and method C calls method A. This is called mutual recursion.
Interesting Examples of Recursion
Recursion Components
- One or more base cases. A base case is a situation where
the answer is known immediately.
- A general case. To solve the general case, one or more recursive
calls must be made to solve easier problems. The answers to these
easier problems must then be combined.
Recursion Examples
- Computing triangle numbers.
- Generating permutations.
- Checking if a string is a palindrome.
Lecture Code