Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree Problems #66

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Binary Tree/Check_If_All_Leaf_Nodes_Are_At_Same_Level.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
Problem Statement:

Given a Binary Tree, check if all leaves are at same level or not.

Code:

#include <bits/stdc++.h>
using namespace std;

struct Node {
int data;
Node *left, *right;
};

Node* newNode(int data){
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}

int checkLevelLeafNode(Node* root){
if (!root)
return 1;

queue<Node*> q;
q.push(root);

int result = INT_MAX;
int level = 0;

while (!q.empty()) {
int size = q.size();
level += 1;

while(size > 0){
Node* temp = q.front();
q.pop();

if (temp->left) {
q.push(temp->left);

if(!temp->left->right && !temp->left->left){

if (result == INT_MAX)
result = level;

else if (result != level)
return 0;
}
}

if (temp->right){
q.push(temp->right);

if (!temp->right->left && !temp->right->right)
if (result == INT_MAX)
result = level;
else if(result != level)
return 0;

}
size -= 1;
}
}

return 1;
}

int main(){
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->right = newNode(4);
root->right->left = newNode(5);
root->right->right = newNode(6);

int result = checkLevelLeafNode(root);
if (result)
cout << "All leaf nodes are at same level\n";
else
cout << "Leaf nodes not at same level\n";
return 0;
}


Time Complexity : O(N)

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Problem Statement:

Given the array representation of Complete Binary Tree i.e, if index i is the parent, index 2*i + 1 is the left child and index 2*i + 2 is the right child.
The task is to find the minimum number of swap required to convert it into Binary Search Tree.

Code:

#include<bits/stdc++.h>
using namespace std;

void inorder(int a[], vector<int> &v, int n, int index){

if(index >= n)
return;
inorder(a, v, n, 2 * index + 1);

v.push_back(a[index]);
inorder(a, v, n, 2 * index + 2);
}

int minSwaps(vector<int> &v){
vector<pair<int,int> > t(v.size());
int ans = 0;
for(int i = 0; i < v.size(); i++)
t[i].first = v[i], t[i].second = i;

sort(t.begin(), t.end());
for(int i = 0; i < t.size(); i++){

if(i == t[i].second)
continue;
else{

swap(t[i].first, t[t[i].second].first);
swap(t[i].second, t[t[i].second].second);
}

if(i != t[i].second)
--i;
ans++;
}
return ans;
}


int main(){
int a[] = { 5, 6, 7, 8, 9, 10, 11 };
int n = sizeof(a) / sizeof(a[0]);
vector<int> v;
inorder(a, v, n, 0);
cout << "Minimum swaps required to convert binary tree to BST : "<<minSwaps(v) << endl;
}

Input : arr[] = { 5, 6, 7, 8, 9, 10, 11 }
Output : 3

Time Complexity : O(nlogn)