Codacoda
Back to Academy

patterns

Recursion

Recursion solves problems by having a function call itself with a smaller input until it reaches a base case. Every recursive solution needs a base case to stop and a recursive case that breaks the problem down. Understanding the call stack and how each frame stores its own variables is key to mastering recursive thinking.

Use Cases

  • Tree and graph traversal algorithms
  • Mathematical computations like factorial and Fibonacci
  • Parsing nested structures like JSON or HTML

Complexity Analysis

MetricBestAverageWorst
TimeO(n)O(n)O(2^n)
SpaceO(n)

Visualization

5432101234
Call:fact(5)
Speed:1x
Compute factorial(5). fact(n) = n * fact(n-1), base case: fact(1) = 1Step 1 / 10

Implementation

Output

Click "Run Code" to see output...