#include <bits/stdc++.h>
using
namespace
std;
struct
tNode {
int
data;
struct
tNode* left;
struct
tNode* right;
};
struct
tNode* newtNode(
int
data)
{
struct
tNode* node =
new
tNode;
node->data = data;
node->left = NULL;
node->right = NULL;
return
(node);
}
int
flag = 0;
void
findCommonNodes(
struct
tNode* root,
int
K1,
int
K2)
{
struct
tNode *current, *pre;
if
(root == NULL)
return
;
current = root;
while
(current != NULL) {
if
(current->left == NULL) {
if
(current->data == K1 || current->data == K2) {
if
(flag) {
return
;
}
else
{
flag = 1;
}
}
else
if
(flag) {
cout << current->data <<
" "
;
}
current = current->right;
}
else
{
pre = current->left;
while
(pre->right != NULL
&& pre->right != current)
pre = pre->right;
if
(pre->right == NULL) {
pre->right = current;
current = current->left;
}
else
{
pre->right = NULL;
if
(current->data == K1 || current->data == K2) {
if
(flag) {
return
;
}
else
{
flag = 1;
}
}
else
if
(flag) {
cout << current->data <<
" "
;
}
current = current->right;
}
}
}
}
int
main()
{
struct
tNode* root = newtNode(1);
root->left = newtNode(12);
root->right = newtNode(11);
root->left->left = newtNode(3);
root->right->left = newtNode(4);
root->right->right = newtNode(13);
root->right->left->right = newtNode(15);
root->right->right->left = newtNode(9);
int
K1 = 12, K2 = 15;
findCommonNodes(root, K1, K2);
return
0;
}