AP-Computer-Science ScienceAP
Recursion Recursion
A method calls itself to derive a solution to a problem.
Solving a recursive problem
- Find the base cases. Think about the simplest inputs whose answers are immediately known.
- Take the recursive leap of faith. Identify the recursive behavior, usually by calling the method on a smaller input. Trust the smaller subproblem’s result when constructing the current result.
- Recombine. Build the answer to the original problem from those results.
Each recursive call must make progress toward a base case; otherwise the recursion may never terminate normally.
Related: Methods · Recursive base conversions · Time complexity