Appearance
How to solve recursion problems?
- Break problem into sub-problems.
- If required - write a recursive relation formula.
- If function requires to return value then always return from recursion.
- See how the values are returned at each step.
- ** Value is returned to the calling function.
- Define a base case for breaking a recursive call.
- Visualize the recursion tree
- See the flow of the function, how they are getting in the stack.
- Draw the tree and pointer using pen & paper (practice)
- In tree, identify flow of left tree and right tree calls.
- See where the function call will come out. In the end you will come out of the main function call.
- If variable needs to be required in the future state then always pass to the function or create outside of the function scope.
- Always, make sure to return the result of the function call of the return type.
Type of recurrence relation
- Linear recurrence - like fibonacci, countdown etc.
- Divide & Conquer - like Binary Search