CCA Guide
Recursion

Recursion

Curated by Last modified: 1 min read

View Markdown

A method calls itself to derive a solution to a problem.

Solving a recursive problem

  1. Find the base cases. Think about the simplest inputs whose answers are immediately known.
  2. 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.
  3. 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

Linked to this page