Recursion & Backtracking
Anower Hossain
1 min read
1-Recursion-Basic
#include "bits/stdc++.h"
using namespace std;
void solve(int n)
{
if(n==0) return;
solve(n-1);
cout<< n << " ";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
solve(n);
return 0;
}
input
======
3
output
=======
1 2 3
2-Tree-Recursion
#include "bits/stdc++.h"
using namespace std;
void solve(int n)
{
if(n==0) return;
cout<< n << " ";
solve(n-1);
solve(n-1);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
solve(n);
return 0;
}
input
======
3
output
=======
3 2 1 1 2 1 1
0
Subscribe to my newsletter
Read articles from Anower Hossain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Anower Hossain
Anower Hossain
This is Anower Hossain. Passionate about programming, and problem-solving.