The Six Core Patterns of Recursion
Recursion made easy by applying one of these six patterns onto every question.
Recursion, you may be slightly uncomfortable just by hearing the term. One of the core reasons people find recursion so tricky is that they don’t have an exact model of interpreting recursion as an approach.
People solve various recursive problems and see different approaches to using it, but still don’t feel confident about using it. It’s just because every situation is entirely different that it makes it hard to figure out what’s going on.
This article will go through 6 recursive core patterns that may categorize all-recursive problems into one of them. You can apply them to almost any recursive questions.
These six patterns are — Iteration, Subproblems, Selection, Ordering, Divide & Conquer, and Depth First Search.
Iteration
As you may know, any problem that can be solved using loops can also be solved using recursion in place of it and vice-versa. It may not come handy often, but can be useful once in a while. We can use this pattern when we refer back to the items we’ve looped through previously.
For example, if we want to print a linked list in reverse order.
void printReverse(Node n) {
if (n == null) return…