Skip to main content
CSAlgorithms & ComplexityDiagramBeginner

Recursion

KO: 재귀 (Recursion)·EN: Recursion·JA: 再帰(Recursion)

A technique where a function calls itself on smaller subproblems until reaching a base case, naturally expressing tree and divide-and-conquer logic

Table of Contents

Structure Diagram

Recursion Call Expansion & Base-Case Unwinding Cycle
RECURSION1. Recursive…2. Hit Base …3. Unwind Ca…4. Return Fi…
1/41. Recursive Call
Push frame f(n-1)
Quick Answer

What is Recursion?

A technique where a function calls itself on smaller subproblems until reaching a base case, naturally expressing tree and divide-and-conquer logic. It is a core concept in Algorithms & Complexity within CS.

Core Principles

Recursion solves a problem by having a function invoke itself on progressively smaller inputs until reaching a stopping point. Every sound recursive function has:

  • Base Case: A terminating condition that returns directly without further calls.
  • Recursive Step: A self-call that strictly moves input closer to the base case.

Each invocation pushes a frame onto the runtime Call Stack, unwinding in reverse order once the base case returns.

Practical Takeaways

  • Guard against Stack Overflow: Deeply nested inputs (skewed trees, massive JSON) can exhaust the call stack. Convert deep recursion to an explicit stack loop.
  • Don't assume Tail-Call Optimization (TCO): Functional languages like Scheme/Erlang optimize tail calls, but Python, Java, and mainstream JS engines do not.
  • Watch for exponential blowup: Add memoization (DP) whenever recursive branches recompute identical arguments.

Notable Examples

  • Tree & Filesystem Traversal: Walking directories, DOM nodes, and UI component trees.
  • Compilers & Parsers: Recursive-descent parsing of nested expressions and JSON/AST structures.
  • Divide & Conquer: Merge Sort, Quick Sort, and backtracking search.