Approaching Recursion Problems - Don't think recursion

Опубликовано: 05 Март 2026
на канале: HeadEasyLabs
265
8

Approaching Recursion

Don't Think Recursion to apply recursion !! Don't think too much

So, we talked about how a problem is solved recursively.How the calls happen. But an interesting point about approaching a recursive problem is that, you actually don't need to think recursively while solving the problem.

You don’t need to know what’s happening in every step. If you want to start solving recursive problems, you must be willing to take a leap of a faith. You have to believe. Assumptions will need to be made and is necessary for solving these types of problems.

There are few steps that can be followed for writing a recursive solution :
1. What the function should return and what the user is going to provide to calculate that result.

function sumTo(n) {
}


the function should return an integer sum from 1-n.


2. Pick a subproblem and just assume that your function already works on it.


A sub problem is a problem that is exactly identical to the original problem but smaller than the original problem.

We also talked about how to divide a problem in previous lecture.

Ex. If the top level call is for n. Then sub problem can be for n-1.
This is a level-1 problem.

function sum(n) { // n is our original problem
// Using n-1 as our subproblem, it returns the sum from 1 to n-1.
const solutionToSubproblem = sum(n-1)
}