Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <iostream>

using namespace std;


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

node* newnode(int data)


{
node* Node = (node*)malloc(sizeof(node));
Node->data = data;
Node->left = Node->right = NULL;
return (Node);
}
node* array2tree(int arr[], node* root,int k, int n)
{
if (k < n)
{
node* temp = newnode(arr[k]);
root = temp;
root->left = array2tree(arr,root->left, 2 * k + 1, n);
root->right = array2tree(arr,root->right, 2 * k + 2, n);
}
return root;
}

bool isSameParent(node* root,int x,int y){


if(root==NULL){
return false;
}else{
if(root->left!=NULL&&root->right!=NULL){
if(root->left->data==x && root->right->data==y){
return true;
}else if(root->left->data==y && root->right->data==x){
return true;
}
}
if(root->left!=NULL){
if(isSameParent(root->left,x,y)){
return true;
}
}
if(root->right!=NULL){
if(isSameParent(root->right,x,y)){
return true;
}
}
}
}

bool search(node* root,int val){


if(root==NULL){
return false;
}else if(root->data==val){
return true;
}else{
if(search(root->left,val)){
return true;
}else if(search(root->right,val)){
return true;
}
}
}
void isvalidteam(node* root,int s1, int s2){
if(search(root,s1)&&search(root,s2)&&(!isSameParent(root,s1,s2))){
std::cout << "Valid Team members" << std::endl;
}else{
std::cout << "Invalid Team members" << std::endl;
}
}

int main(){
int arr[7]={1,2,3,4,5,6,7};
node* root=array2tree(arr,root,0,7);
std::cout << "Enter the Team members" << std::endl;
for(int i=0;i<4;i++){
int a,b;
cout<<"Team member1: ";cin>>a;
cout<<"Team member2: ";cin>>b;
isvalidteam(root,a,b);
}
return 0;
}

You might also like