Recursion method by definitions is a way of solving a problem by having a function calling itself and make the problems become smaller and easier to find solutions.
Why We Need Recursion?
1. Recursion can break big problems into smaller ones and easy to use
2. Prominent usage of recursion in data structures like trees and graphs
3. Used in many algorithm (divide and conquer, greedy and dynamic programming)
How it works?
1. The method call it self
2. There's end conditions to interrupt loop
3. In stack Memory using FILO.
Recursive Vs Iterative Solutions?
Stack memory require?Recursive: Yes
Iterative: No
Time Efficient?
Recursive: No
Iterative: Yes
Easy To Code?
Recursive: Yes
Iterative: No
When To Use Recursion:
1. When conditions breakdown to similar small sub problems meet
2. When Fine with extra overhead
3. When need quick working solutions
4. When traverse a tree
5. When use memorization in recursion
Method to do Recursion:
Factorial Problems: 5! = 5*4*3*2*1 => n * (n-1) * (n-2) * (n-3) ... *2 * 1
Define Formula: n! = n * f(n-1)
0 comments:
Post a Comment