# Types of Recursions

**What is Recursion?**

The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.

**Types of Recursions:**

Recursion are mainly of** two types** depending on whether **a function calls itself from within itself** or **more than one function call one another mutually.** The first one is called **direct recursion** and another one is called **indirect recursion**. Thus, the two types of recursion are:

**Direct Recursion**: These can be further categorized into**four types**:**Tail Recursion**: If a recursive function calling itself and that recursive call is the last statement in the function then it’s known as**Tail Recursion.**After that call the recursive function performs nothing. The function has to process or perform any operation at the time of calling and it does nothing at returning time.**Example:**

`// Code Showing Tail Recursion`

`#include <stdio.h>`

`// Recursion function`

`void`

`fun(`

`int`

`n)`

`{`

`if`

`(n > 0) {`

`printf`

`(`

`"%d "`

`, n);`

`// Last statement in the function`

`fun(n - 1);`

`}`

`}`

`// Driver Code`

`int`

`main()`

`{`

`int`

`x = 3;`

`fun(x);`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**3 2 1

Let’s understand the example by

**tracing tree of recursive function.**That is how the calls are made and how the outputs are produced.**Time Complexity For Tail Recursion : O(n)**

**Space Complexity For Tail Recursion : O(n)****Note:**Time & Space Complexity is given for this specific example. It may vary for another example.Lets’s now converting Tail Recursion into Loop and compare each other in terms of Time & Space Complexity and decide which is more efficient.

`// Converting Tail Recursion into Loop`

`#include <stdio.h>`

`// Recursive function`

`void`

`fun(`

`int`

`n)`

`{`

`while`

`(n > 0) {`

`printf`

`(`

`"%d "`

`, n);`

`n--;`

`}`

`}`

`// Driver code`

`int`

`main()`

`{`

`int`

`x = 3;`

`fun(x);`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**3 2 1

**Time Complexity: O(n)**

**Space Complexity: O(1)****Note:**Time & Space Complexity is given for this specific example. It may vary for another example.So it was seen that in case of loop the Space Complexity is O(1) so it was better to write code in loop instead of tail recursion in terms of Space Complexity which is more efficient than tail recursion.

**Head Recursion**: If a recursive function calling itself and that recursive call is the first statement in the function then it’s known as**Head Recursion.**There’s no statement, no operation before the call. The function doesn’t have to process or perform any operation at the time of calling and all operations are done at returning time.**Example:**

`// C program showing Head Recursion`

`#include <stdio.h>`

`// Recursive function`

`void`

`fun(`

`int`

`n)`

`{`

`if`

`(n > 0) {`

`// First statement in the function`

`fun(n - 1);`

`printf`

`(`

`"%d "`

`, n);`

`}`

`}`

`// Driver code`

`int`

`main()`

`{`

`int`

`x = 3;`

`fun(x);`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**1 2 3

Let’s understand the example by

**tracing tree of recursive function.**That is how the calls are made and how the outputs are produced.**Time Complexity For Head Recursion: O(n)**

**Space Complexity For Head Recursion: O(n)****Note:**Time & Space Complexity is given for this specific example. It may vary for another example.**Note:**Head recursion can’t easily convert into loop as Tail Recursion but it can.Let’s convert the above code into the loop.`// Converting Head Recursion into Loop`

`#include <stdio.h>`

`// Recursive function`

`void`

`fun(`

`int`

`n)`

`{`

`int`

`i = 1;`

`while`

`(i <= n) {`

`printf`

`(`

`"%d "`

`, i);`

`i++;`

`}`

`}`

`// Driver code`

`int`

`main()`

`{`

`int`

`x = 3;`

`fun(x);`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**1 2 3

**Tree Recursion**: To understand**Tree Recursion**let’s first understand**Linear Recursion**. If a recursive function calling itself for one time then it’s known as**Linear Recursion**. Otherwise if a recursive function calling itself for more than one time then it’s known as**Tree Recursion**.**Example:****Pseudo Code for linear recursion**fun(n) { if(n>0) { // some code fun(n-1); // Calling itself only once { //some code }

**Program for tree recursion**

`// C program to show Tree Recursion`

`#include <stdio.h>`

`// Recursive function`

`void`

`fun(`

`int`

`n)`

`{`

`if`

`(n > 0) {`

`printf`

`(`

`"%d "`

`, n);`

`// Calling once`

`fun(n - 1);`

`// Calling twice`

`fun(n - 1);`

`}`

`}`

`// Driver code`

`int`

`main()`

`{`

`fun(3);`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**3 2 1 1 2 1 1

Let’s understand the example by

**tracing tree of recursive function.**That is how the calls are made and how the outputs are produced.**Time Complexity For Tree Recursion: O(2^n)**

**Space Complexity For Tree Recursion: O(n)****Note:**Time & Space Complexity is given for this specific example. It may vary for another example.**Nested Recursion**: In this recursion, a recursive function will pass the parameter as a recursive call.That means**“recursion inside recursion”.**Let see the example to understand this recursion.**Example:**`// C program to show Nested Recursion`

`#include <stdio.h>`

`int`

`fun(`

`int`

`n)`

`{`

`if`

`(n > 100)`

`return`

`n - 10;`

`// A recursive function passing parameter`

`// as a recursive call or recursion`

`// inside the recursion`

`return`

`fun(fun(n + 11));`

`}`

`// Driver code`

`int`

`main()`

`{`

`int`

`r;`

`r = fun(95);`

`printf`

`(`

`"%d\n"`

`, r);`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**91

**tracing tree of recursive function.**That is how the calls are made and how the outputs are produced.

**Indirect Recursion**: In this recursion, there may be more than one functions and they are calling one another in a circular manner.From the above diagram fun(A) is calling for fun(B), fun(B) is calling for fun(C) and fun(C) is calling for fun(A) and thus it makes a cycle.

**Example:**`// C program to show Indirect Recursion`

`#include <stdio.h>`

`void`

`funB(`

`int`

`n);`

`void`

`funA(`

`int`

`n)`

`{`

`if`

`(n > 0) {`

`printf`

`(`

`"%d "`

`, n);`

`// Fun(A) is calling fun(B)`

`funB(n - 1);`

`}`

`}`

`void`

`funB(`

`int`

`n)`

`{`

`if`

`(n > 1) {`

`printf`

`(`

`"%d "`

`, n);`

`// Fun(B) is calling fun(A)`

`funA(n / 2);`

`}`

`}`

`// Driver code`

`int`

`main()`

`{`

`funA(20);`

`return`

`0;`

`}`

*chevron_right**filter_none***Output:**20 19 9 8 4 3 1

**tracing tree of recursive function.**That is how the calls are made and how the outputs are produced.

## Recommended Posts:

- Find the parent of a node in the given binary tree
- Java Program to Calculate Power of a Number
- Generating all divisors of a number using its prime factorization
- Maximum height of the binary search tree created from the given array
- Count number of ways to arrange first N numbers
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- C++ Program to print an Array using Recursion
- Add the given digit to a number stored in a linked list using recursion
- Divide an array into K subarray with the given condition
- Twisted Tower of Hanoi Problem
- Compute the maximum power with a given condition
- Maximum Sequence Length | Collatz Conjecture
- Find Maximum Level Sum in Binary Tree using Recursion
- Optimal Strategy for a Game | Set 3

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.