您的位置:首页 > 其它

POJ_2309_BST(位运算)

2014-03-10 19:09 357 查看
题型:数据结构

题意:对于二叉搜索树,给出一个节点,求出该节点的最左子孙和最右子孙的节点号。

分析:

运用树状数组里所使用的压位运算,可以轻松的算出节点所管辖的范围,即:

[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: