Pattern Studioalgorithmic thinking
All patterns

Recursion

Backtracking & Recursion
Beginner
Interactive

Watch recursive calls branch, reach a base case, and unwind.

Time
Depends on the recurrence
Space
O(recursion depth)
ActiveComparingMatch / bestIn windowFrontierVisitedExcludedAnswer
No frames to display.

The idea

Recursion solves a problem by delegating a smaller version of the same problem to a new call frame. Choose among several examples to see linear call stacks, branching call trees, recursive accumulation, pointer rewiring during linked-list reversal, and structural recursion through a binary search tree.

Analogy

This visualization makes the state transitions behind recursion explicit, one decision at a time.

When to reach for it

  • The input is recursive by nature, such as a linked list or tree.
  • The problem reduces to smaller instances with a clear base case.
  • Unwinding naturally combines results or rewires structure.

Language-independent template

define a base case
reduce to one or more smaller subproblems
combine returned answers while the call stack unwinds

Complexity — time Depends on the recurrence, space O(recursion depth).

Common mistakes

  • Choosing the wrong state or invariant.
  • Updating state before preserving the value needed next.
  • Missing a boundary or base case.

Practice progression

Canonical interview problems, ordered from foundation to advanced application.

Easy

Reverse a linked list recursively.

Medium

Swap every adjacent pair without changing node values.

Hard

Reverse linked-list nodes in complete groups of k.

Variations

Linear recursion · factorial and array sum
Branching recursion · Fibonacci
Linked-list recursion · reverse pointers on unwind
Tree recursion · choose and search a child subtree