You are given a binary search tree with depth k, whose nodes are valued from 1 to (2k − 1) and then Q queries.
For each query, you are given p nodes. Find the root of a smallest subtree which contains all p nodes and print its
value.
1
4 1
3 10 15 13
12
#include
#include
#include
比赛的时候想复杂了。通过将二叉搜索树上的值转化为二进制,然后找到了规律,每次都将当前的两个数转化为二进制,然后找到第一个不同的二进制位,先判断置为0可否,再判断了置为1可否。超时。
超时代码
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
ll arr[maxn];int n=4,m;
ll getone(ll pre, ll old){
if(pre == old) return pre;
if(pre >old) swap(pre,old);
ll ppre = pre;
ll pold = old;
vectorv1;
vectorv2;
while(pre){
v1.push_back(pre%2);
pre>>=1;
}
while(old){
v2.push_back(old%2);
old>>=1;
}
while(v1.size() < n) v1.push_back(0);
while(v2.size() < n) v2.push_back(0);
// reverse(v1.begin(),v1.end());
// reverse(v2.begin(),v2.end());
ll ans = 0;
int k = n-1;
//
// for(int i=0;i=0;i--){
if(v1[i] != v2[i]){
if(ans >= ppre && ans<= pold){
return ans;
}else{
ll ans1 = ans + (1<