Skip to content

How to solve recursion problems? ​

  1. Break problem into sub-problems.
  2. If required - write a recursive relation formula.
  3. If function requires to return value then always return from recursion.
  4. See how the values are returned at each step.
    • ** Value is returned to the calling function.
  5. Define a base case for breaking a recursive call.
  6. 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.
  7. See where the function call will come out. In the end you will come out of the main function call.
  8. If variable needs to be required in the future state then always pass to the function or create outside of the function scope.
  9. Always, make sure to return the result of the function call of the return type.

Type of recurrence relation ​

  1. Linear recurrence - like fibonacci, countdown etc.
  2. Divide & Conquer - like Binary Search

Tutorials ​