DSa Problems Cheatsheet
DSa Problems Cheatsheet
if(root==NULL)
return;
res.push_back(root->data);
preOrderList(root->left,res);
preOrderList(root->right,res);
vector<int> res;
preOrderList(root,res);
return res;
vector<int> res;
inOrderTraversal(root,res);
return res;
if(root==NULL)
return;
inOrderTraversal(root->left,res);
res.push_back(root->data);
inOrderTraversal(root->right,res);
// code here
if(node==NULL)
return 0;
return max(height(node->left),height(node->right))+1;
//base case
if (node==NULL) return;
visited[pathLen] = false;
pathLen++;
//if it's a leaf node, we increment the count but only if the
counter++;
visited[pathLen-k-1] = true;
return;
//if the current node is not a leaf node then we call the function
int counter = 0;
return counter;
{
vector<vector<int>> ans;
queue<Node *> q;
q.push(node);
int size;
while(!q.empty())
size = q.size();
vector<int> aux1;
for(int i = 0;i<size;i++)
aux = q.front();
q.pop();
aux1.push_back(aux->data);
if(aux->left!=NULL)
q.push(aux->left);
if(aux->right!=NULL)
q.push(aux->right);
ans.push_back(aux1);
return ans;
// Code here
vector<int> ans;
stack<int> s;
queue<Node *> q;
q.push(root);
int size;
while(!q.empty())
size = q.size();
for(int i = 0;i<size;i++)
{
aux = q.front();
q.pop();
if(direction=="right")
ans.push_back(aux->data);
else
s.push(aux->data);
if(aux->left!=NULL)
q.push(aux->left);
if(aux->right!=NULL)
q.push(aux->right);
if(direction=="left")
while(!s.empty())
ans.push_back(s.top());
s.pop();
direction="right";
else
direction="left";
return ans;
// code here
vector<int> ans;
queue<Node *> q;
int size;
q.push(root);
while(!q.empty())
{
size = q.size();
for(int i = 0;i<size;i++)
aux = q.front();
q.pop();
if(i==0)
ans.push_back(aux->data);
if(aux->left!=NULL)
q.push(aux->left);
if(aux->right!=NULL)
q.push(aux->right);
return ans;
if(ifSum(root))
return 1;
else
return 0;
if(root==NULL)
return true;
if(root->left==NULL&&root->right==NULL)
return true;
int sum=0;
if(root->left!=NULL)
sum +=root->left->data;
if(root->right!=NULL)
sum+=root->right->data;
if(sum==root->data&&ifSum(root->left)&&ifSum(root->right))
return true;
}
void mirror(Node* node) {
// code here
if(node==NULL)
return;
node->left = node->right;
node->right = aux;
mirror(node->left);
mirror(node->right);
if(root==NULL)
return true;
int lh = height(root->left);
int rh = height(root->right);
if(isBalanced(root->left)&&isBalanced(root->right)&&abs(lh-rh)<=1)
return true;
if(root==NULL)
return 0;
return 1+max(height(root->left),height(root->right));
if(T==NULL)
return false;
if(S==NULL)
return true;
if(checkIdentical(T,S))
return true;
return isSubTree(T->left,S)||isSubTree(T->right,S);
{
if(T==NULL&&S==NULL)
return true;
if(T==NULL||S==NULL)
return false;
return (T->data==S->data&&checkIdentical(T->right,S->right)&&checkIdentical(T->left,S->left));
if(r1==NULL&&r2==NULL)
return true;
if(r1==NULL||r2==NULL)
return false;
return isMirror(r1->left,r2->right)&&isMirror(r1->right,r2->left);
if(root==NULL)
return true;
return isMirror(root->left,root->right);
if (root == NULL) {
return 0;
while (!q.empty()) {
q.pop();
if (node->left) {
if (node->right) {
return verticalLines.size();
if (!root) return;
if (prev == NULL) {
head = root; // The first node becomes the head of the DLL
} else {
return head;
// code here
unordered_map<int, int> m;
Node* aux = new Node(curr); // Create a new node with the current root value
int index = 0;
while(aux!=NULL)
index++;
aux = aux->next;
}
index = 0;
aux = head;
while(aux!=NULL)
if(m.find(2*index+1)!=m.end())
m[index]->left = m[2*index+1];
if(m.find(2*index+2)!=m.end())
m[index]->right = m[2*index+2];
aux = aux->next;
index++;
root = m[0];
sumHelper(root,res);
return res;
if(root==NULL)
return 0;
res = max(res,max_top);
return single_sum;
}
if(root==NULL)
return NULL;
if(root->data==n1||root->data==n2)
return root;
if(lh!=NULL&&rh!=NULL)
return root;
else if(rh!=NULL&&lh==NULL)
return rh;
else if(rh==NULL&&lh!=NULL)
return lh;
else
return NULL;
if(root==NULL)
return INT_MAX;
if(root->right==NULL&&root->left==NULL)
return root->data;
max_diff = max(max_diff,root->data-min(lsmall,rsmall));
return min(root->data,min(lsmall,rsmall));
maxDiffHelper(root,max_diff);
return max_diff;
if(root==NULL)
return 0;
if(root->data+lsum+rsum==X)
count++;
return root->data+lsum+rsum;
int count = 0;
countHelper(root,X,count);
return count;
if (root == NULL)
return 0;
count++;
int count = 0;
countHelper(root, X, count);
return count;
BST
bool checkBST(Node *root, int lbound, int rbound)
if(root==NULL)
return true;
return(root->key>lbound&&root->key<rbound&&checkBST(root->left,lbound,root->key)&&checkBST(root->right,root->key,rbound));
int i = 0;
while(i<inorder.size())
if(inorder[i]!=correct[i])
break;
i++;
cout << "The 2 nodes to be swapped is: " << first << " and " << second;
if(root==NULL)
return;
inOrderTraversal(root->left,inorder);
inorder.push_back(root->key);
inOrderTraversal(root->right,inorder)
vector<int> inOrder;
inOrderTraversal(root,inOrder);
int i = 0;
int j = inOrder.size()-1;
while(j>=i)
if(inOrder[i]+inOrder[j]==sum)
return true;
else if(inOrder[i]+inOrder[j]<sum)
i++;
else
j--;
return false;
if(root==NULL)
return;
verticalTree(root->left,mp,hd-1);
mp[hd]=mp[hd]+root->key;
verticalTree(root->right,mp,hd+1);
map<int,int> mp;
verticalTree(root,mp,0);
for(auto sum:mp)
queue<pair<Node *,int>> q;
map<int,vector<int>> mp;
q.push({root,0});
while(!q.empty())
auto p = q.front();
q.pop();
mp[hd].push_back(aux->key);
if(aux->left!=NULL)
q.push({aux->left,hd-1});
if(aux->right!=NULL)
q.push({aux->right,hd+1});
for(auto values:mp)
for(auto val:aux)
queue<pair<Node *,int>> q;
map<int,vector<int>> mp;
q.push({root,0});
while(!q.empty())
auto p = q.front();
q.pop();
int hd = p.second;
mp[hd].push_back(aux->key);
if(aux->left!=NULL)
q.push({aux->left,hd-1});
}
if(aux->right!=NULL)
q.push({aux->right,hd+1});
for(auto values:mp)
return root;
if (!root) {
root = node;
return;
while (temp) {
prev = temp;
temp = temp->left;
}
else if (temp->val < key) {
prev = temp;
temp = temp->right;
prev->left = node;
else
prev->right = node;
current = current->left;
return current;
// base case
else
if (root->left == NULL)
free(root);
return temp;
free(root);
return temp;
root->key = temp->key;
return root;
if (!root)
return INT_MAX;
if (root->data == key)
return root->data;
/* If root->data is greater than the key */
// base case
if (root == NULL)
return NULL;
if (left != NULL)
return left;
count++;
if (count == k)
return root;
Linked List
Node *insertAtBegin(Node *head, int x)
temp->next = head;
head->prev = temp;
return temp;
while((curr->next)!=NULL)
curr = curr->next;
temp->prev = curr;
curr->next = temp;
return head;
Node *temp;
curr->prev = curr->next;
curr->next = NULL;
curr = curr->prev;
while((curr->next)!=NULL)
temp = curr->next;
curr->next = curr->prev;
curr->prev = temp;
curr = curr->prev;
curr->next = curr->prev;
curr->prev = NULL;
return curr;
curr = curr->next;
curr->prev = NULL;
delete head;
return curr;
while((curr->next->next)!=NULL)
curr = curr->next;
delete curr->next;
curr->next = NULL;
return head;
while(curr!=NULL)
curr = curr->next;
int curr_pos = 1;
while(curr_pos!=pos-1)
curr = curr->next;
curr_pos++;
temp->next = curr->next;
curr->next = temp;
return head;
}
head = head->next;
return head;
while(curr->next->next!=NULL)
curr = curr->next;
curr->next = NULL;
return head;
int pos = 1;
while((curr!=NULL)&&(curr->data)!=x)
curr = curr->next;
pos++;
if(curr!=NULL)
return pos;
else
return -1;
Sorting
void bubbleSort(int arr[], int n) {
for(int i = 0;i<n-1;i++)
for(int j = 0;j<n-i-1;j++)
if(arr[j]>arr[j+1])
swap(arr[j],arr[j+1]);
int n = arr.size();
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
return arr;
int n1 = A.size();
int n2 = B.size();
int n3 = C.size();
vector<int> arr(n1+n2,0);
int i = 0;
int j = 0;
int index = 0;
if(A[i]<=B[j])
arr[index] = A[i];
i++;
else
arr[index] = B[j];
j++;
index++;
while(i<n1)
arr[index] = A[i];
i++;
index++;
while(j<n2)
arr[index] = B[j];
j++;
index++;
vector <int>ans(n1+n2+n3,0);
index = 0;
i = 0;
j = 0;
if(arr[i]<=C[j])
ans[index] = arr[i];
i++;
else
{
ans[index] = C[j];
j++;
index++;
while(i<n1+n2)
ans[index] = arr[i];
i++;
index++;
while(j<n3)
ans[index] = C[j];
j++;
index++;
return ans;
if(high>low)
int p = partition(arr,low,high);
quickSort(arr,low,p-1);
quickSort(arr,p+1,high);
public:
int i = low-1;
int j = low;
while(j<high)
{
if(arr[j]<pivot)
i++;
swap(arr[i],arr[j]);
j++;
i++;
swap(arr[i],arr[j]);
return i;
// code here
int l = 0;
int mid = 0;
while(high>=mid)
mid++;
else if(array[mid]<a)
swap(array[l],array[mid]);
l++;
mid++;
else
swap(array[high],array[mid]);
high--;
int i = 0, j = 0, k = left;
arr[k++] = leftArr[i++];
} else {
arr[k++] = rightArr[j++];
arr[k++] = leftArr[i++];
arr[k++] = rightArr[j++];