文章

3

粉丝

0

获赞

9

访问

253

头像
二叉树2 题解:
P1264 北京大学机试题
发布于2026年3月11日 11:38
阅读数 86

递归,边界条件:1.当前权值大于n,不满足条件,返回0  2.当前权重小于等于n,则左右递归计算子树满足条件的结点数求和,再+1(当前结点权值满足<=n条件)

#include <iostream>
using namespace std;
int count(int a,int n)
{
if(a>n) {return 0;}
else {return count(2*a,n)+count(2*a+1,n)+1;}
}
int main()
{
 int m,n;
 while(cin>>m>>n)
 { 
  int y=count(m,n);
     cout<<y<<endl;
 }
}

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发