POJ 2309 BST(二叉搜索树)

思路:除以2^k,找到商为奇数的位置,k为层数,有2^(k+1)-1个节点

这里直接用位运算,x & -x 就求出 2^k 了。

 

#include<iostream>
using namespace std;

long lowbit(long x)
{
	return x & -x;
}
int main()
{
	long n,x;
	cin>>n;
	while(n--)
	{
		cin>>x;
		cout<<x-lowbit(x)+1<<' '<<x+lowbit(x)-1<<endl;
	}
	return 0;
}


 

posted @ 2014-07-31 12:11  gongpixin  阅读(163)  评论(0编辑  收藏  举报