对于一棵满二叉排序树深度为k,节点数为2^k – 1,节点值为1至(2^k – 1),给出k和任意三个节点的值,输出包含该三个节点的最小子树的根节点值。样例输入:4 10 15 13,样例输出:12
#include "iostream"
#include "vector"
#include "math.h"
using namespace std;
int main3()
{
int dep = 0;
cin >> dep;
vector<int> invec;
for (int i = 0; i < 3; i++)
{
int temp;
cin >> temp;
invec.push_back(temp);
}
int value = pow((double)2, (double)(dep - 1));
int out = 0;
for (int i = dep - 1; i > 0; i--)
{
if (invec[0] > value && invec[1] > value && invec[2] > value)
value = value + pow((double)2, (double)(i-1));
else if (invec[0] < value && invec[1] < value && invec[2] < value)
value = value - pow((double)2, (double)(i-1));
else
{
out = value;
break;
}
}
cout << out << endl;
system("pause");
return 0;
}
8
4 12
2 6 10 14
1 3 5 7 9 11 13 15