-
Notifications
You must be signed in to change notification settings - Fork 8
/
check-two-nodes-cousins-binary-tree.cpp
executable file
·68 lines (61 loc) · 1.52 KB
/
check-two-nodes-cousins-binary-tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// http://www.geeksforgeeks.org/check-two-nodes-cousins-binary-tree/
#include <bits/stdc++.h>
using namespace std;
// struct node of binary tree
struct node{
int data;
node *left, *right;
};
// return new node
node *getnode(int data){
node *temp = new node();
temp->data = data;
temp->left = temp->right = NULL;
}
int checkIfSibling(node *root, int p1, int p2){
if(!root) return 0;
if(root->left && root->right){
if((root->left->data == p1 && root->right->data == p2) ||
(root->left->data == p2 && root->right->data == p1)) return 1;
}
return (checkIfSibling(root->left,p1,p2) || checkIfSibling(root->right,p1,p2));
}
int checkCousin(node *root, int p1, int p2){
int a = -1, b = -1;
queue<node*> q;
if(!root) return 0;
q.push(root);
while(!q.empty()){
int size = q.size();
a = -1, b = -1;
while(size--){
node *temp = q.front();
q.pop();
if(temp->data == p1)
a = 1;
else if(temp->data == p2)
b = 1;
if(temp->left) q.push(temp->left);
if(temp->right) q.push(temp->right);
}
if(a == 1 && b == 1 && !checkIfSibling(root,p1,p2))
return 1;
}
return 0;
}
int main(){
//contruct binary tree
node *root = getnode(1);
root->left = getnode(2);
root->left->left = getnode(4);
root->left->right = getnode(5);
root->left->left->right = getnode(2);
root->right = getnode(3);
root->right->right = getnode(8);
root->right->right->left = getnode(6);
root->right->right->right = getnode(7);
int p1 = 4, p2 = 8;
if(checkCousin(root,p1,p2)) cout << "yes\n";
else cout << "No\n";
return 0;
}