Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Recursion Patterns
DSA

Recursion Patterns

Learn the fundamental recursive problem-solving patterns used in DSA and coding interviews.

Master Recursion Patterns for DSA + Competitive Programming

This guide helps you recognize recursion problems instantly using:

  • input structure (array, tree, string, graph)
  • recursion type (decision / generation / divide & conquer)
  • keywords (subset, choice, path, split, return all, min/max)

Recursion becomes easy once you identify:

“What choices do I have at this step?” → “What smaller problem remains?”


Pattern Table (Simplified & High-Yield)

PatternTypical Question TypesKeywords / Detection CuesNotes / When to Use
Decision Tree / Choice RecursionSubsets, combinations, permutationspick / not pick, choose, all possibilitiesAt each index, make a binary or multi-choice decision
Divide & ConquerSplit problem into halvesmerge, sort, maximum, minimumBreak into subproblems, combine results (merge sort style)
Tree RecursionBinary trees / N-ary treesleft, right, node, leaf, subtreeEach node naturally branches into recursive calls
DFS on Graph/GridExplore connected componentsvisited, island, path, flood fillRecursion replaces stack; mark visited to avoid cycles
String RecursionSubstring generation, parsingsubstring, partition, palindromeSplit string at every index or decision boundary
Optimization Recursion (Min/Max)Best path, best costminimum, maximum, optimal, costExplore all paths, return best among recursive calls

1. Decision Tree / Choice Recursion (Most Important)

When to use / Detection cues:

  • Input: array, string, list of items
  • Keywords: subset, combination, permutation, pick/not pick
  • Problem asks: “all possible ways”

Core idea: At each index → you have choices

  • include element
  • exclude element

Typical questions:

  • Subsets of an array
  • Combinations sum
  • Permutations

Mental trigger: “Every element → take or skip” → Decision Tree recursion


2. Divide & Conquer (Split + Merge)

When to use / Detection cues:

  • Problem can be split into equal halves
  • Keywords: merge, sort, max/min range
  • Input is array or numeric range

Core idea:

  • Divide into smaller subproblems
  • Solve recursively
  • Combine results

Typical questions:

  • Merge Sort
  • Quick Sort
  • Binary Search (recursive version)
  • Maximum subarray (Kadane’s divide variant)

Mental trigger: “Split → Solve → Combine” → Divide & Conquer


3. Tree Recursion (Natural Recursion Structure)

When to use / Detection cues:

  • Input: binary tree / N-ary tree
  • Keywords: node, left, right, subtree

Core idea: Each node naturally calls:

  • left child
  • right child

Typical questions:

  • Tree traversal (inorder, preorder, postorder)
  • Height of tree
  • Diameter of tree
  • Lowest common ancestor

Mental trigger: “Node → left + right recursion” → Tree recursion


4. DFS on Graph/Grid (Traversal Recursion)

When to use / Detection cues:

  • Input: matrix or graph
  • Keywords: visited, island, connected, path
  • Movement allowed in directions (up/down/left/right)

Core idea:

  • Mark visited
  • Explore neighbors recursively

Typical questions:

  • Number of islands
  • Flood fill
  • Maze path existence
  • Connected components

Mental trigger: “Explore all connected nodes” → DFS recursion


5. String Recursion (Split / Partition Problems)

When to use / Detection cues:

  • Input: string
  • Keywords: substring, partition, palindrome, split

Core idea: Try splitting string at every index and recurse on remaining part

Typical questions:

  • Palindrome partitioning
  • Generate all substrings
  • Restore IP addresses
  • Word break (recursive version)

Mental trigger: “Cut string at every position” → String recursion


6. Optimization Recursion (Min / Max Problems)

When to use / Detection cues:

  • Keywords: minimum, maximum, best, optimal
  • Multiple recursive paths exist
  • Need best result among all choices

Core idea:

  • Explore all possibilities
  • Return best value using min/max

Typical questions:

  • Minimum path sum
  • Maximum profit path
  • Coin change (recursive version)
  • Knapsack (recursive version)

Mental trigger: “Try all paths → pick best” → Optimization recursion


Mini Notes / Tips

### Recursion Master Rules

- Every recursion has 3 parts:
  1. Base case (stop condition)
  2. Choice (what decisions exist?)
  3. Recursive call (smaller problem)

- If you see "all possible ways" → Decision Tree
- If input is a tree → natural recursion
- If grid/matrix → DFS recursion
- If array split → Divide & Conquer
- If best/min/max → Optimization recursion
- If string → think split at every index

### Golden Thinking Pattern

STATE → CHOICES → RECURSE → COMBINE → RESULT

My Private Notes

Notes are auto-saved locally to this device.