Important Questions on Binary Trees
Question 1 Find diameter of a binary tree
#include<iostream>
#include<stdio.h>
#include<queue>
#include<math.h>
using namespace std;
struct bstNode{
int data;
bstNode *left;
bstNode *right;
};
bstNode *getNewNode(int d){
bstNode *start=new bstNode();
start->data=d;
start->left=NULL;
start->right=NULL;
return start;
}
bstNode *insert(bstNode *root,int d){
if(root==NULL){
root=getNewNode(d);
return root;
}
else if(root->data>=d){
root->left=insert(root->left,d);
return root;
}
else if(root->data<d){
root->right=insert(root->right,d);
return root;
}
}
int height(bstNode *root){
int leftheight,rightheight;
if(root==NULL){
return -1;
}
leftheight=height(root->left);
rightheight=height(root->right);;
return max(leftheight,rightheight)+1;
}
int count=0;
void levelOrder(bstNode *root,int space){
queue<bstNode *> q;
q.push(root);
count++;
int i=0;
int x=space;
while(!q.empty()){
x=x/2;
bstNode *current=q.front();
for(i=0;i<x;i++){
cout<<" ";
}
cout<<current->data<<"\t";
count--;
for(i=0;i<x;i++){
cout<<" ";
}
if(current->left!=NULL){
q.push(current->left);
}
if(current->right!=NULL){
q.push(current->right);
}
q.pop();
if(count==0){
count=q.size();
cout<<endl;
}
}
cout<<"\n";
for(i=0;i<x;i++){
cout<<" ";
}
}
int Diameter(bstNode * root, int& h){
int hl=0,hr=0,dl,dr,dia;
if(root==NULL){
h=0;
return 0;
}
dl=Diameter(root->left,hl);
dr=Diameter(root->right,hr);
if(hl>hr){
h=hl+1;
}else{
h=hr+1;
}
h=max(hl,hr)+1;
int x=hl+hr+1,m;
//cout<<"\nRoot -> Data"<<root->data<<" Hl = "<<hl<<" Hr = "<<hr<<" DL = "<<dl<<" DR = "<<dr;
if(dl>dr){
m=dl;
}else{
m=dr;
}
if(m>x){
dia=m;
}else{
dia=x;
}
return dia;
}
main(){
bstNode *root;
int c=4;
root=NULL;
root=insert(root,30);
root=insert(root,15);
root=insert(root,40);
root=insert(root,35);
root=insert(root,45);
root=insert(root,10);
root=insert(root,20);
root=insert(root,9);
root=insert(root,12);
root=insert(root,13);
root=insert(root,21);
root=insert(root,22);
root=insert(root,23);
root=insert(root,24);
root=insert(root,50);
root=insert(root,60);
root=insert(root,70);
//levelOrder(root,x);
cout<<"\nDiameter is = "<<Diameter(root,c);
}
#include<stdio.h>
#include<queue>
#include<math.h>
using namespace std;
struct bstNode{
int data;
bstNode *left;
bstNode *right;
};
bstNode *getNewNode(int d){
bstNode *start=new bstNode();
start->data=d;
start->left=NULL;
start->right=NULL;
return start;
}
bstNode *insert(bstNode *root,int d){
if(root==NULL){
root=getNewNode(d);
return root;
}
else if(root->data>=d){
root->left=insert(root->left,d);
return root;
}
else if(root->data<d){
root->right=insert(root->right,d);
return root;
}
}
struct xy{
int x, y;
};
xy max_sum(bstNode *root){
xy i,i1,i2;
if(root==NULL){
i.x=0;
i.y=0;
return i;
}
i1=max_sum(root->left);
i2=max_sum(root->right);
i.x=i1.y+i2.y+root->data;
i.y=i1.x+i2.x;
return i;
}
main(){
bstNode *root;
int h=0;;
root=NULL;
root=insert(root,1000);
root=insert(root,1001);
root=insert(root,100);
root=insert(root,100);
xy instance =max_sum(root);
int x=instance.x,y=instance.y,m;
m=max(x, y);
cout<<"\nmax sum is = "<<m;
}
Question 2 Given a Binary tree check if it is balanced i.e. depth of the left and right subtrees of every node differ by 1 or less
#include<iostream>
#include<stdio.h>
#include<queue>
#include<math.h>
using namespace std;
struct bstNode{
int data;
bstNode *left;
bstNode *right;
};
bstNode *getNewNode(int d){
bstNode *start=new bstNode();
start->data=d;
start->left=NULL;
start->right=NULL;
return start;
}
bstNode *insert(bstNode *root,int d){
if(root==NULL){
root=getNewNode(d);
return root;
}
else if(root->data>=d){
root->left=insert(root->left,d);
return root;
}
else if(root->data<d){
root->right=insert(root->right,d);
return root;
}
}
int height(bstNode *root){
int leftheight,rightheight;
if(root==NULL){
return -1;
}
leftheight=height(root->left);
rightheight=height(root->right);;
return max(leftheight,rightheight)+1;
}
bool checkBST(bstNode *root, int& h){
int hl=0, hr=0, diff, max;
bool x1, x2;
if(root==NULL){
h=-1;
return 1;
}
x1=checkBST(root->left,hl);
x2=checkBST(root->right,hr);
if(hl>=hr){
diff=hl-hr;
max=hl;
}else{
diff=hr-hl;
max=hr;
}
if((x1&x2)&&(diff<=1)){
h=max+1;
return 1;
}else{
return 0;
}
}
main(){
bstNode *root;
int h=0;;
root=NULL;
root=insert(root,30);
root=insert(root,15);
root=insert(root,40);
root=insert(root,35);
root=insert(root,45);
root=insert(root,10);
root=insert(root,20);
root=insert(root,9);
root=insert(root,12);
root=insert(root,8);
root=insert(root,13);
root=insert(root,21);
root=insert(root,50);
/* root=insert(root,22);
root=insert(root,23);
root=insert(root,24);
root=insert(root,50);
root=insert(root,60);
root=insert(root,70);
*/
bool x=checkBST(root,h);
if(x){
cout<<"\nTree is balanced";
}else{
cout<<"\nTree is NOT balanced";
}
}
Question 3 You have a binary tree with non-negative numeric values stored in its nodes, you need to find out the maximum possible sum of all the nodes. Selection of nodes for the sum follows following constraint: If you have selected any node for the sum, you can not select its immediate parent and its children for sum.
#include<iostream>#include<stdio.h>
#include<queue>
#include<math.h>
using namespace std;
struct bstNode{
int data;
bstNode *left;
bstNode *right;
};
bstNode *getNewNode(int d){
bstNode *start=new bstNode();
start->data=d;
start->left=NULL;
start->right=NULL;
return start;
}
bstNode *insert(bstNode *root,int d){
if(root==NULL){
root=getNewNode(d);
return root;
}
else if(root->data>=d){
root->left=insert(root->left,d);
return root;
}
else if(root->data<d){
root->right=insert(root->right,d);
return root;
}
}
struct xy{
int x, y;
};
xy max_sum(bstNode *root){
xy i,i1,i2;
if(root==NULL){
i.x=0;
i.y=0;
return i;
}
i1=max_sum(root->left);
i2=max_sum(root->right);
i.x=i1.y+i2.y+root->data;
i.y=i1.x+i2.x;
return i;
}
main(){
bstNode *root;
int h=0;;
root=NULL;
root=insert(root,1000);
root=insert(root,1001);
root=insert(root,100);
root=insert(root,100);
xy instance =max_sum(root);
int x=instance.x,y=instance.y,m;
m=max(x, y);
cout<<"\nmax sum is = "<<m;
}