题型:数据结构
题意:对于二叉搜索树,给出一个节点,求出该节点的最左子孙和最右子孙的节点号。
分析:
运用树状数组里所使用的压位运算,可以轻松的算出节点所管辖的范围,即:
[n-(n&(-n)),n+(n&(-n))]
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int Lowbit(int t) {
return t & -t;
}
int main(){
int t,n;
while(~scanf("%d",&t)){
while(t--){
scanf("%d",&n);
int x = Lowbit(n) - 1;
printf("%d %d\n",n-x,n+x);
}
}
return 0;
}