判断满二叉树中有不同子树的节点有多少个
来源:互联网 发布:微信恶搞软件 编辑:程序博客网 时间:2024/05/21 09:31
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
5678
Sample Output
3435
题目将给你一个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
- 判断满二叉树中有不同子树的节点有多少个
- n个节点的二叉树有多少种形态
- N个节点的二叉树有多少种形态
- N个节点的二叉树有多少种形态
- N个节点的二叉树有多少种形态
- N个节点的二叉树有多少种形态
- N个节点的二叉树有多少种形态
- !HDU 4359 左子树节点和小于右子树的树有多少种-dp
- 800个节点的完全二叉树共有多少个叶子节点?相反,已知叶子节点,求最多有多少个节点!
- 具有N个节点的二叉树有多少种形态,居然有计算公式
- N个节点二叉树有多少种形态
- 每日一练——N个节点的平衡二叉树有多少种形态
- 判断一个节点是否在二叉树中,判断tree2是否为tree1的子树
- BC#62C题求二叉树不同节点数的子树的个数
- 【HDU5524 BC61 div1 B】【SET模拟】Subtrees 二叉树不同子节点树的子树个数
- 面试题:完全二叉树699个节点,则叶子节点有多少个?
- 顺序二叉树,求m下面有多少个节点(总共n个节点)
- 2个二叉树A、B,树的每个节点上都有一个整数值。如何判断A与B是否其中一个是另一个的子树?
- HDU 5769 Substring(后缀数组)
- WRMSR--写MSR
- UISegmentedControl搭配UIScrollView使用
- RDMSR--读MSR
- [AIZU1361] Deadlock Detection [2015 ACM-ICPC Asia Tsukuba Regional Contest F]
- 判断满二叉树中有不同子树的节点有多少个
- 九度OJ 1076 N的阶乘 (大整数运算)
- LeetCode 91 Decode Ways
- 基于Python的二叉树遍历算法
- HandlerThread解析
- LightOJ 1422 Halloween Costumes 区间DP
- Java 或 Android属性生成工具
- Bloom
- Java设计模式的六大原则