[go: up one dir, main page]

0% found this document useful (0 votes)
19 views7 pages

Optus Notes 2

self made notes

Uploaded by

riddhishukla71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Optus Notes 2

self made notes

Uploaded by

riddhishukla71
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Trees:

#include <iostream>

using namespace std;

class node{

public:

int val;

node* left;

node* right;

node(int data){

val=data;

};

int main(){

node* root= new node(10);

node* n1= new node(20);

node* n2= new node(30);

root ->left = n1;

root ->right = n2;

cout<<root->val<< " "<<n1->val<<" "<<n2->val;

return 0;

Bases cases determined when we have to stop our code.

Rec (______________)

Base case

Logic

Return.

 RECURRENCE RELATION
 TREE/GRAPH
 DECIDION MAKING BASED ON OPTIONS.

Recursion is basically used in Fibonacci series.

Fib(n)= fib(n-1) +fib(n-2)

Recusion is making a stack back of it.

1. long long factorial(long long n)

//base case

if(n==1){

return 1;

//logic

long long ans=1;

ans=n*factorial(n-1);

//return statement

return ans;

int main(){

int n;

cin>>n;

long long fac= factorial(n);

cout<<fac;

return 0;

2. #include<iostream>
using namespace std;

int recSum(int n){


//base condition
if (n<=1)
return n;

//recursive call
return n+ recSum(n-1);
}
int main(){
int n=10;

cout << "Sum = " << recSum(n) << endl;

return 0;

3.

#include<iostream>

using namespace std;

int sumOfSquares(int n){

int sum =0;

for(int i=1; i<=n; i++)

sum += (i*i);

return sum;

int main(){

int n=2;

cout<< sumOfSquares(n);

return 0;

5.

#include<iostream>

using namespace std;

bool isPalin(string &s, int i, int j){

//base case

if(i>=j){
return ans;

if (s[i] != s[j]) {

return false;

//logic

bool ans = isPalin(s, i-1, j-1);

return ans;

int main(){

string s= "abbcbba";

cout<< isPalin(s,0,s.size()-1);

return 0;

Question 2 advance level:

#include<iostream>

using namespace std;

int rec(int prev, int 0, int z){

if(o<0 pr z<0){

return 0;

if(o==0 and z==0){

return 1;

int ans=0;

if(prev == 0){

ans = rec(0, o, z-1) + rec(1, 0-1, z);

} else{
ans = rec(0, o, z-1);

return ans;

int main(){

int o = 2, z=3; // given o>0 and z>0

cout<<rec(0,o,z-1)+ rec(1,o-1,z);

return 0;

Question 3 advance level:

#include<iostream>

using namespace std;

int rec(int prev, int idx, vector<int, &nums){

//base case

if(idx>=nums.size()){

return 0;

int ans =0;

if(prev ==1){

ans= max(rec(1, idx+1, nums), rec(2, idx+2, nums));

}else{

ans= rec(1, idx+1,nums);

return ans + nums[idx];

int main(){

vector<int>nums = {2,-5,3,6,-12,4};

cout<< max(rec(1,0,nums), rec(2,-1,nums));

return 0;
}

Trees.

INORDER TRAVERSAL

#include <iostream>

using namespace std;

class solution{

public:

vector<int> inorderTraversal (TreeNode* root){

vector<int>v;

trav(root,v);

return v;

void trav(TreeNode* root, vector<int> &v){

if(root == nullptr){

return;

trav(root->left, v);

v.push_back(root->val);

trav(root->right, v);

};

int i =0 , j = nums.size() -1;


if(nums[0] == val){
nums.pop_back();
return 0;
}
}
while (i<=j){
if(nums[i] == val){
swap(nums[i], nums[j]);
j--;
nums.pop_back();
}
else i++;
}
return nums.size();
}
};

You might also like