题意:给出一棵二分搜索树,再给一个节点编号n,求以这个节点为根节点的子树叶子节点的最大值与最小值。

 

首先求n所在的层数,他的层数就是他的因子中2的个数(规律). n的左右各有num=2^i-1个数。最小值是n-num,最大值是n+num

 

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>

using namespace std;
int t,n;

int finds(int n){
    for(int i=0;i<=30;i++){
        if((1<<i)<=n && n<(1<<(i+1)))
            return i;
    }
}
int main()
{
    scanf("%d",&t);
    for(int i=1;i<=t;i++){
        scanf("%d",&n);
        int k=0;
        int tmp=n;
        while(1){
            if(tmp%2==0){
                k++;
                tmp=tmp/2;
            }
            else{
                break;
            }
        }
        int mins=n-((1<<k)-1);
        int maxs=n+((1<<k)-1);
        printf("%d %d\n",mins,maxs);
    }
    return 0;
}

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案