#include <bits/stdc++.h>
using
namespace
std;
struct
Node {
int
key;
struct
Node *left, *right;
};
Node* newNode(
int
key)
{
Node* temp =
new
Node;
temp->key = key;
temp->left = temp->right = NULL;
return
(temp);
}
struct
Node* findLCA(
struct
Node* root,
int
n1,
int
n2)
{
if
(root == NULL)
return
NULL;
if
(root->key == n1 || root->key == n2)
return
root;
Node* left_lca = findLCA(root->left, n1, n2);
Node* right_lca = findLCA(root->right, n1, n2);
if
(left_lca && right_lca)
return
root;
return
(left_lca != NULL) ? left_lca : right_lca;
}
Node* findNode(Node* root,
int
a,
int
b,
int
c)
{
Node* x = findLCA(root, a, b);
Node* y = findLCA(root, b, c);
Node* z = findLCA(root, c, a);
if
(x->key == y->key)
return
z;
else
if
(x->key == z->key)
return
y;
else
return
x;
}
int
main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->left->left->left = newNode(8);
root->left->left->right = newNode(9);
root->left->right->left = newNode(10);
root->left->right->right = newNode(11);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->right->left->left = newNode(12);
root->right->left->right = newNode(13);
root->right->right->left = newNode(14);
root->right->right->right = newNode(15);
cout <<
"Disconnect node "
<< findNode(root, 5, 6, 15)->key
<<
" from the tree"
;
return
0;
}