当前位置:网站首页>子树的大小
子树的大小
2022-08-03 19:46:00 【-JMY-】
题目描述

有如上图所示的完全二叉树,该二叉树有n个结点,编号从上向下、从左向右以此为1~n。请问,
编号为m的结点所在的子树,包含了多少个结点?
比如,n = 12,m = 3,则上图中的结点13,14,15以及后面的结点都是不存在的,结点m=3所在
子树中包括的结点有3,6,7,12,因此结点m的所在子树中共有4个结点。
输入
输出
样例输入
3 7
样例输出
3
参考代码:
#include<bits/stdc++.h>
using namespace std;
int n,m,s=1;
void down(int x){
if(x*2+1<=n){
s+=2;
down(x*2);
down(x*2+1);
}else if(x*2<=n){
s++;
down(x*2);
}
return;
}
int main(){
cin>>m>>n;
down(m);
cout<<s;
return 0;
}
边栏推荐
- FreeRTOS中级篇
- 【STM32】标准库-自定义BootLoader
- Power button brush the topic of merging two orderly array
- docker mysql 容器中执行mysql脚本文件并解决乱码
- 告诉你0基础怎么学好游戏建模?
- The effective square of the test (one question of the day 7/29)
- redis常用命令,HSET,XADD,XREAD,DEL等
- LeetCode 622. Designing Circular Queues
- Jingdong cloud released a new generation of distributed database StarDB 5.0
- dpkg强制安装软件
猜你喜欢
随机推荐
2022 CCF中国开源大会会议通知(第三轮)
Word另存为PDF后无导航栏解决办法
演讲议题及嘉宾重磅揭晓,TDengine 开发者大会推动数据技术“破局”
高性能计算软件与开源生态| ChinaOSC
盘点在线帮助中心对企业能够起到的作用
C中的数据存储
Unity获取canvas 下ui 在屏幕中的实际坐标
Jingdong cloud released a new generation of distributed database StarDB 5.0
Detailed AST abstract syntax tree
【leetcode】剑指 Offer II 007. 数组中和为 0 的三个数(双指针)
不要再用if-else
高位套牢机构,用友网络的信任危机是如何产生的?
ECCV 2022 Oral | 满分论文!视频实例分割新SOTA: IDOL
List类的超详细解析!(超2w+字)
Climbing Stairs (7/30)
Execute the mysql script file in the docker mysql container and solve the garbled characters
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃
The sword refers to Offer II 044. The maximum value of each level of the binary tree-dfs method
glide set gif start stop
The ecological environmental protection management system based on mobile GIS









