Recursion

Recursion is a concept in which a method calls itself continuously. Any function which calls itself is recursive, and such function calls are recursive calls. It helps solve problems that can be broken down into smaller, repetitive problems provided one or more base conditions that stop the recursion.

Advantages –

  • The code is simpler and shorter.
  • More readable and efficient algorithms.
  • Reduce time complexity.
  • Better for tree and graph traversal.
  • Avoid unnecessary repetitive blocks of code.

Syntax –

returntype methodname(){
	//code
	methodname(); //calling same method
}

Types of Recursion –

  • Direct Recursion – A function calls itself directly.
    • Tail Recursion – The last statement in the function is a recursive call.
    • Head Recursion – The recursive call is the first statement in the function.
    • Tree Recursion – A recursive function calling itself for more than one time.
    • Nested Recursion –  A recursive function that passes a recursive call as its parameter.
  • Indirect Recursion – A function A calls function B and function B calls function A.
Scroll to Top