#include <bits/stdc++.h>
using
namespace
std;
struct
node {
int
data;
node* left;
node* right;
node(
int
data)
{
this
->data = data;
left = NULL;
right = NULL;
}
};
void
next(stack<node*>& it)
{
node* curr = it.top()->right;
it.pop();
while
(curr != NULL)
it.push(curr), curr = curr->left;
}
int
minDiff(node* root1, node* root2)
{
stack<node *> it1, it2;
node* curr = root1;
while
(curr != NULL)
it1.push(curr), curr = curr->left;
curr = root2;
while
(curr != NULL)
it2.push(curr), curr = curr->left;
int
ans = INT_MAX;
while
(it1.size() and it2.size()) {
int
v1 = it1.top()->data;
int
v2 = it2.top()->data;
ans = min(
abs
(v1 - v2), ans);
if
(v1 < v2)
next(it1);
else
next(it2);
}
return
ans;
}
int
main()
{
node* root2 =
new
node(5);
root2->left =
new
node(3);
root2->right =
new
node(7);
root2->left->left =
new
node(2);
root2->left->right =
new
node(4);
root2->right->left =
new
node(6);
root2->right->right =
new
node(8);
node* root1 =
new
node(11);
root1->right =
new
node(15);
cout << minDiff(root1, root2);
return
0;
}