当前位置:网站首页>子树的大小
子树的大小
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;
}
边栏推荐
- Climbing Stairs (7/30)
- 基于移动GIS的环保生态管理系统
- 【飞控开发高级教程6】疯壳·开源编队无人机-AI语音控制
- The addition and subtraction of the score of the force deduction brush question (a daily question 7/27)
- net-snmp私有mib动态加载到snmpd
- 622 设计循环队列——Leetcode天天刷【循环队列,数组模拟,双指针】(2022.8.2)
- 【夜莺监控方案】08-监控msyql集群(prometheuse+n9e+mysqld_exporter)
- Statistical machine learning 】 【 linear regression model
- Network protocol-TCP, UDP difference and TCP three-way handshake, four wave
- Radondb mysql installation problems
猜你喜欢

揭秘5名运维如何轻松管理数亿级流量系统

安装anaconda并创建虚拟环境

不知道这4种缓存模式,敢说懂缓存吗?

JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof

虚拟机vmware设置桥接模式上网

简易电子琴设计(c语言)

【飞控开发高级教程6】疯壳·开源编队无人机-AI语音控制

149. The largest number on a straight line, and check the set

Line the last time the JVM FullGC make didn't sleep all night, collapse

NNLM、RNNLM等语言模型 实现 下一单词预测(next-word prediction)
随机推荐
不知道这4种缓存模式,敢说懂缓存吗?
按需视觉识别:愿景和初步方案
Word另存为PDF后无导航栏解决办法
LeetCode 952. 按公因数计算最大组件大小
Radondb mysql installation problems
开源教育论坛| ChinaOSC
力扣刷题之求两数之和
开源生态研究与实践| ChinaOSC
1161 最大层内元素和——Leetcode天天刷【BFS】(2022.7.31)
群辉查看硬盘存储占用的方式
Reveal how the five operational management level of hundreds of millions of easily flow system
LeetCode 622. 设计循环队列
力扣刷题之移动零
CS kill-free pose
php根据两点经纬度计算距离
Force is brushed buckle problem for the sum of two Numbers
JWT详解
友宏医疗与Actxa签署Pre-M Diabetes TM 战略合作协议
Climbing Stairs (7/30)
详解AST抽象语法树