DEV Community

Andy Chen
Andy Chen

Posted on

RECURSION RECAP

Define Recursion. - Calls itself

Identify when writing a recursive algorithm is the right approach to solving a problem.

Can replace a for loop or when we don't know when we want the function to end. (Linkedlist node). Nested data structures identical structure nested inside. Combination / permutation then use recursion. (n is the input).

Identify the base case of a recursive algorithm/strategy.
bold*-All recursive functions have at least one base case; a case where the function can produce a result without recursing.

In the factorial example above, number === 0 is an example of this solution's base case: If number is 0, we know the solution must be 1 and thus we don't need to recursive to solve the problem.

Identify the recursive case of a recursive algorithm/strategy.
-recursive case is where function invokes itself.

Understand what the Call Stack is.
-Call stack is the order in which the function is executed. It starts LIFO LAST IN FIRST OUT

Understand the relationship between the execution contexts that come to exist during a recursive process.

STEPS TO SOLVE RECURSION
1) Identify the smallest piece of the data that your function needs to handle
2) Write the function to handle this case and only this case (BASE CASE).
3) Once base case handled, Identify what will make the function need to continue (find the recursive call)
4) Make the recursive call
5) Accumulate the return to previous calls (store the value)

Top comments (0)