判断满二叉树中有不同子树的节点有多少个


Problem Description
There is a complete binary tree with N nodes.The subtree of the node i has Ai nodes.How many distinct numbers are there of Ai?
 

Input
There are multiple test cases, no more than 1000 cases. For each case contains a single integer N on a line.$(1\leq N\leq {10}^{18})$
 

Output
The output of each case will be a single integer on a line:the number of subtrees that contain different nodes.
 

Sample Input
  
  
5 6 7 8
 

Sample Output
  
  
3 4 3 5
 
题意再说一遍:

题目将给你一个n,告诉你一棵完全二叉树有多少个节点。在这颗完全二叉树中,每个节点都有不同的size(即其下包涵多少child),要求你判断题中给的完全二叉树中有多少个不同的节点。

对于满二叉树来说,它的答案一定是这个满二叉树的深度。

对于非满完全二叉树来说,每每往下推进一层,题中所求就会增加1

那么求出这颗完全二叉树中的满二叉树最大层数,以及有多少颗非满二叉树

下面是代码:


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <stack>
using namespace std;
long long maxn;
long long ans;
long long n;
void find(long long x){
    long long dep=1;
    long long l=x,r=x;
    while(l*2<=n){
        l=l*2;
        dep++;
    }
    while(r*2+1<=n){
        r=r*2+1;
    }
    if(l<=r){
        maxn=max(maxn,dep);
    }else{
        find(x*2);
        find(x*2+1);
        ans++;
    }
}
int main(){
    while(~scanf("%lld",&n)){
        maxn=0;
        ans=0;
        find(1);
        cout<<ans+maxn<<endl;
    }
    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值