Recursion & Backtracking
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