#include <bits/stdc++.h>
using
namespace
std;
struct
Node {
int
data;
struct
Node *left, *right;
};
struct
Node* newNode(
int
item)
{
struct
Node* temp =
new
Node();
temp->data = item;
temp->left = temp->right = NULL;
return
temp;
}
int
findCousinSum(Node* root,
int
key)
{
if
(root == NULL)
return
-1;
if
(root->data == key) {
return
-1;
}
int
currSum = 0;
int
size;
queue<Node*> q;
q.push(root);
bool
found =
false
;
while
(!q.empty()) {
if
(found ==
true
) {
return
currSum;
}
size = q.size();
currSum = 0;
while
(size) {
root = q.front();
q.pop();
if
((root->left && root->left->data == key)
|| (root->right && root->right->data == key)) {
found =
true
;
}
else
{
if
(root->left) {
currSum += root->left->data;
q.push(root->left);
}
if
(root->right) {
currSum += root->right->data;
q.push(root->right);
}
}
size--;
}
}
return
-1;
}
int
main()
{
struct
Node* root = newNode(1);
root->left = newNode(3);
root->right = newNode(7);
root->left->left = newNode(6);
root->left->right = newNode(5);
root->left->right->left = newNode(10);
root->right->left = newNode(4);
root->right->right = newNode(13);
root->right->left->left = newNode(17);
root->right->left->right = newNode(15);
cout << findCousinSum(root, 13) <<
"\n"
;
cout << findCousinSum(root, 7) <<
"\n"
;
return
0;
}