Types of Recursions
Types of Recursions
Types of Recursions
Array Matrix Strings Hashing Linked List Stack Queue Binary Tree Binary Search Tree
What is Recursion?
The process in which a function calls itself directly or indirectly is called recursion and
cer tain problems can be solved quite easily. Examples of such problems are Towers of
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
1. Direct Recursion: These can be fur ther 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. Af ter that call the
recursive function per forms nothing. The function has to process or per form any
Example :
C++
https://www.geeksforgeeks.org/types-of-recursions/ 1/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
void fun(int n)
{
if (n > 0) {
cout << n << " ";
// Driver Code
int main()
{
int x = 3;
fun(x);
return 0;
}
#include <stdio.h>
// Recursion function
void fun(int n)
{
if (n > 0) {
printf("%d ", n);
// Driver Code
int main()
{
int x = 3;
fun(x);
return 0;
}
https://www.geeksforgeeks.org/types-of-recursions/ 2/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
Java
// Recursion function
static void fun(int n)
{
if (n > 0)
{
System.out.print(n + " ");
// Driver Code
public static void main(String[] args)
{
int x = 3;
fun(x);
}
}
P ython3
# Recursion function
def fun(n):
if (n > 0):
print(n, end=" ")
# Last statement in the function
fun(n - 1)
# Driver Code
x = 3
fun(x)
https://www.geeksforgeeks.org/types-of-recursions/ 3/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
C#
class GFG
{
// Recursion function
static void fun(int n)
{
if (n > 0)
{
Console.Write(n + " ");
// Driver Code
public static void Main(string[] args)
{
int x = 3;
fun(x);
}
}
Javascript
<script>
// Javascript code Showing Tail Recursion
// Recursion function
function fun(n)
{
if (n > 0)
{
document.write(n + " ");
https://www.geeksforgeeks.org/types-of-recursions/ 4/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
// Driver Code
var x = 3;
fun(x);
Output :
3 2 1
Let ’s understand the example by tracing tree of recursive function. That is how the
Note : Time & Space Complexity is given for this specific example. It may var y for
another example.
Lets’s now conver ting Tail Recursion into Loop and compare each other in terms of Time
https://www.geeksforgeeks.org/types-of-recursions/ 5/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
void fun(int y)
{
while (y > 0) {
cout << y << " ";
y--;
}
}
// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}
#include <stdio.h>
void fun(int y)
{
while (y > 0) {
printf("%d ", y);
y--;
}
}
// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}
https://www.geeksforgeeks.org/types-of-recursions/ 6/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
// Driver code
public static void main(String[] args)
{
int x = 3;
fun(x);
}
}
P ython3
# Driver code
x = 3
fun(x)
C#
https://www.geeksforgeeks.org/types-of-recursions/ 7/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
{
while (y > 0) {
Console.Write(" "+ y);
y--;
}
}
// Driver code
public static void Main(String[] args)
{
int x = 3;
fun(x);
}
}
// This code is contributed by shivanisinghss2110
Javascript
<script>
function fun(y)
{
while (y > 0) {
document.write(" "+ y);
y--;
}
}
// Driver code
var x = 3;
fun(x);
Output :
3 2 1
Login
We use cookies to ensure you have the best browsing experience on our website. By using Register
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Space Complexity: O(1) Got It !
Policy
https://www.geeksforgeeks.org/types-of-recursions/ 8/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
Note : Time & Space Complexity is given for this specific example. It may var y 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
Before explaining this I am assuming that you are familiar with the knowledge that ’s
how the data stored in main memor y during execution of a program. In brief,when the
program executes,the main memor y divided into three par ts. One par t for code section,
the second one is heap memor y and another one is stack memor y. Remember that the
program can directly access only the stack memor y, it can’t directly access the heap
In case of loop when function “(void fun(int y))” executes there only one activation
record created in stack memor y(activation record created for only ‘ y ’ variable) so it
takes only ‘one’ unit of memor y inside stack so it ’s space complexity is O(1) but in case
of recursive function ever y time it calls itself for each call a separate activation record
created in stack.So if there’s ‘n’ no of call then it takes ‘n’ unit of memor y inside stack so
Head Recursion: If a recursive function calling itself and that recursive call is the
statement, no operation before the call. The function doesn’t have to process or
per form any operation at the time of calling and all operations are done at returning
time.
Example :
C++
#include <bits/stdc++.h>
using namespace std;
Start Your Coding Journey Now! Login
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
// Recursive function
Policy
void fun(int n)
https://www.geeksforgeeks.org/types-of-recursions/ 9/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
{
if (n > 0) {
// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}
#include <stdio.h>
// Recursive function
void fun(int n)
{
if (n > 0) {
// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}
Start Your Coding Journey Now! Login
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy
https://www.geeksforgeeks.org/types-of-recursions/ 10/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
Java
class GFG{
// Recursive function
static void fun(int n)
{
if (n > 0) {
// Driver code
public static void main(String[] args)
{
int x = 3;
fun(x);
}
}
P ython3
if (n > 0):
print(n,end=" ")
x = 3
fun(x)
C#
class GFG{
// Recursive function
static void fun(int n)
{
if (n > 0) {
// Driver code
public static void Main(String[] args)
{
int x = 3;
fun(x);
}
}
Javascript
<script>
{
Start Your Coding Journey Now!
function
We use cookies fun(n) Login
to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
if (n > 0) { Policy
https://www.geeksforgeeks.org/types-of-recursions/ 12/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
// Driver code
var x = 3;
fun(x);
</script>
Output :
1 2 3
Let ’s understand the example by tracing tree of recursive function. That is how the
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy
https://www.geeksforgeeks.org/types-of-recursions/ 13/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
Note : Time & Space Complexity is given for this specific example. It may var y for
another example.
Note : Head recursion can’t easily conver t into loop as Tail Recursion but it can. Let ’s
C++
// Recursive function
void fun(int n)
{
int i = 1;
while (i <= n) {
cout <<" "<< i;
i++;
}
}
// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}
// this code is contributed by shivanisinghss2110
#include <stdio.h>
// Recursive function
void fun(int n)
{
int i = 1;
while (i <= n) {
https://www.geeksforgeeks.org/types-of-recursions/ 14/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks
// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}
Java
// Driver code
public static void main(String[] args)
{
int x = 3;
fun(x);
}
}
P ython3
https://www.geeksforgeeks.org/types-of-recursions/ 15/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
i+=1
# Driver code
x = 3
fun(x)
C#
// Driver code
public static void Main(String[] args)
{
int x = 3;
fun(x);
}
}
Javascript
<script>
https://www.geeksforgeeks.org/types-of-recursions/ 16/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
i++;
}
}
// Driver code
var x = 3;
fun(x);
</script>
Output :
1 2 3
Recursion. If a recursive function calling itself for one time then it ’s known as Linear
Recursion. Other wise if a recursive function calling itself for more than one time
Example :
fun(n)
{
// some code
if(n>0)
{
fun(n-1); // Calling itself only once
}
// some code
}
https://www.geeksforgeeks.org/types-of-recursions/ 17/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
// Recursive function
void fun(int n)
{
if (n > 0)
{
cout << " " << n;
// Calling once
fun(n - 1);
// Calling twice
fun(n - 1);
}
}
// Driver code
int main()
{
fun(3);
return 0;
}
#include <stdio.h>
// Recursive function
void fun(int n)
{
if (n > 0) {
printf("%d ", n);
// Calling once
fun(n - 1);
// Calling twice
https://www.geeksforgeeks.org/types-of-recursions/ 18/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
// Driver code
int main()
{
fun(3);
return 0;
}
Java
// Recursive function
static void fun(int n)
{
if (n > 0) {
System.out.print(" "+ n);
// Calling once
fun(n - 1);
// Calling twice
fun(n - 1);
}
}
// Driver code
public static void main(String[] args)
{
fun(3);
}
}
P ython3
https://www.geeksforgeeks.org/types-of-recursions/ 19/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
# Calling once
fun(n - 1)
# Calling twice
fun(n - 1)
# Driver code
fun(3)
C#
// Recursive function
static void fun(int n)
{
if (n > 0) {
Console.Write(" "+ n);
// Calling once
fun(n - 1);
// Calling twice
fun(n - 1);
}
}
// Driver code
public static void Main(String[] args)
{
fun(3);
}
}
https://www.geeksforgeeks.org/types-of-recursions/ 20/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
Javascript
<script>
// Recursive function
function fun(n)
{
if (n > 0) {
document.write(" "+ n);
// Calling once
fun(n - 1);
// Calling twice
fun(n - 1);
}
}
// Driver code
fun(3);
</script>
Output :
3 2 1 1 2 1 1
Let ’s understand the example by tracing tree of recursive function. That is how the
https://www.geeksforgeeks.org/types-of-recursions/ 21/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
Note : Time & Space Complexity is given for this specific example. It may var y 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
Example :
C++
int fun(int n)
{
if (n > 100)
return n - 10;
int main()
{
int r;
r = fun(95);
return 0;
}
#include <stdio.h>
int fun(int n)
{
if (n > 100)
return n - 10;
// Driver code
int main()
{
int r;
r = fun(95);
printf("%d\n", r);
return 0;
}
Java
https://www.geeksforgeeks.org/types-of-recursions/ 23/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
if (n > 100)
return n - 10;
// Driver code
public static void main(String args[])
{
int r;
r = fun(95);
System.out.print(" "+ r);
}
}
// This code is contributed by shivanisinghss2110
P ython3
if (n > 100):
return n - 10
# Driver code
r = fun(95)
print("", r)
C#
https://www.geeksforgeeks.org/types-of-recursions/ 24/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
// Driver code
public static void Main(String []args)
{
int r;
r = fun(95);
Console.Write(" "+ r);
}
}
// This code is contributed by shivanisinghss2110
Javascript
<script>
// Driver code
var r;
r = fun(95);
document.write(" "+ r);
https://www.geeksforgeeks.org/types-of-recursions/ 25/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
Output :
91
Let ’s understand the example by tracing tree of recursive function. That is how the
2. Indirect Recursion: In this recursion, there may be more than one functions and they
https://www.geeksforgeeks.org/types-of-recursions/ 26/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
From the above diagram fun(A) is calling for fun(B), fun(B) is calling for fun(C) and
Example :
C++
void funA(int n)
{
if (n > 0) {
cout <<" "<< n;
void funB(int n)
{
if (n > 1) {
cout <<" "<< n;
// Driver code
int main()
{
funA(20);
return 0;
}
https://www.geeksforgeeks.org/types-of-recursions/ 27/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
#include <stdio.h>
void funA(int n)
{
if (n > 0) {
printf("%d ", n);
void funB(int n)
{
if (n > 1) {
printf("%d ", n);
// Driver code
int main()
{
funA(20);
return 0;
}
Java
class GFG{
https://www.geeksforgeeks.org/types-of-recursions/ 28/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
// Driver code
public static void main (String[] args)
{
funA(20);
}
}
C#
class GFG{
https://www.geeksforgeeks.org/types-of-recursions/ 29/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
// Driver code
public static void Main (String[] args)
{
funA(20);
}
}
P ython3
# Driver code
funA(20)
Javascript
<script>
https://www.geeksforgeeks.org/types-of-recursions/ 30/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
}
}
function funB(n)
{
if (n > 1) {
document.write(n.toFixed(0) + "</br>");
// Driver code
funA(20);
Output :
20 19 9 8 4 3 1
Let ’s understand the example by tracing tree of recursive function. That is how the
https://www.geeksforgeeks.org/types-of-recursions/ 31/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
like to contribute, you can also write an ar ticle using write.geeksforgeeks.org or mail
Like 49
Previous Next
Policy
Minimum of the Maximum
https://www.geeksforgeeks.org/types-of-recursions/ 32/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
19, May 22
Ar ticle Contributed By :
AmiyaRanjanRout
@AmiyaRanjanRout
Load Comments
Company Learn
About Us Algorithms
Careers Data Structures
In Media SDE Cheat Sheet
Contact Us Machine learning
Privacy Policy CS Subjects
Copyright Policy Video Tutorials
News Languages
Python
Top News
Java
Technology
CPP
Work & Career
Golang
Business
C#
Finance
SQL
Lifestyle
Start YourDjango
CodingTutorial
Journey Now! Login
Improve an Article
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge HTML
Register
Got It !
Pick Topics
that you have read and understood our Cookie Policy & Privacy to Write
Policy
CSS Write Interview Experience
https://www.geeksforgeeks.org/types-of-recursions/ 34/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
p
JavaScript Internships
Bootstrap Video Internship
https://www.geeksforgeeks.org/types-of-recursions/ 35/35