#include <iostream>
#include <queue>
using namespace std;
int l1, l2;
/* A binary tree Node has key, pointer to left and right children */
struct Node
{
int key;
struct Node* left, *right;
};
Node * parent1, *parent2;
/* Helper function that allocates a new Node with the
given key and NULL left and right pointers. */
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
void CalcSum(Node *node, int val1, int val2)
{
queue <Node *> Q;
Node *marker = new Node; // Marker node to indicate end of level
int level = 1; // Initialize level number
// Enqueue the only first level node and marker node for end of level
Q.push(node);
Q.push(marker);
// Simple level order traversal loop
while (Q.empty() == false)
{
// Remove the front item from queue
Node *n = Q.front();
Q.pop();
if(node->left->key == val1 || node->right->key == val1)
{
parent1=node;
l1= level+1;
}
if(node->left->key == val2 || node->right->key == val2)
{
parent2=node;
l2= level+1;
}
// Check if end of level is reached
if (n == marker)
{
// print a new line and increment level number
//cout << endl;
level++;
// Check if marker node was last node in queue or
// level number is beyond the given upper limit
//if (Q.empty() == true || level > high) break;
// Enqueue the marker for end of next level
Q.push(marker);
// If this is marker, then we don't need print it
// and enqueue its children
continue;
}
// If level is equal to or greater than given lower level,
// print it
//if (level >= low)
//cout << n->key << " ";
// Enqueue children of non-marker node
if (n->left != NULL) Q.push(n->left);
if (n->right != NULL) Q.push(n->right);
}
cout<<parent1<<" "<<parent2;
}
/* Driver program to test above functions*/
int main()
{
// Let us construct the BST shown in the above figure
struct Node *root = newNode(6);
root->left = newNode(3);
root->right = newNode(5);
root->left->left = newNode(7);
root->left->right = newNode(8);
root->right->left = newNode(1);
root->right->right = newNode(3);
//cout << "Level Order traversal between given two levels is";
CalcSum(root,3,5);
//cout<<sum;
return 0;
}