题目描述:
于一棵满二叉排序树深度为k,节点数为2^k-1;节点值为1至(2^k - 1),给出k和任意三个节点的值,输出包含该三个节点的最小子树的根节点。
样例输入:4 10 15 13
样例输出:12
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int getRoot(const int &k)
{
int res = 1;
for (int i = 0; i < k; i++)
res *= 2;
return res / 2;
}
int main()
{
int deep,a, b, c;
cin >> deep >> a >> b >> c;
int maxv = max(a, max(b, c));
int minv = min(a, min(b, c));
int root = getRoot(deep--);
while (maxv < root || minv > root)
{
while (maxv < root)
root -= getRoot(deep--);
while (minv > root)
root += getRoot(deep--);
}
cout << root << endl;
return 0;
}