1.有点后序遍历的思想,就是先把左子树,右子树的结果算出来,然后合并到根节点。
2.合并时四种情况分类讨论.
3.对于遇到要找的点就可以直接返回,不管另一个点在这个点下面还是在别的子树上,都是正确的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int n, m;
int a[N];
int query(int root,int x,int y){
//cerr<<root<<endl;
if(root>max(x,y))return -1;
if(root==x||root==y)return root;
int l=query(2*root,x,y);
int r=query(2*root+1,x,y);
if(l==-1&&r==-1)return -1;
if(l!=-1&&r!=-1)return root;
if(l!=-1&&r==-1)return l;
if(l==-1&&r!=-1)return r;
}
void solve(){
int x,y;
while(cin>>x>>y,x!=0){
cout<<query(1,x,y)<<endl;
}

}
int main() {
cin.tie(0);

ios::sync_with_stdio(false);

int t;
//cin>>t;
t=1;
while (t--) {
solve();
}
return 0;
}