文章
2
粉丝
135
获赞
3
访问
5.4k
将n视为根 ,然后深搜算法,在每次递归时候进行检测是否超过了m;未超过则cnt+1;超过则返回;最后cnt即为结果
#include <iostream>
using namespace std;
int n, m, cnt = 0;typedef struct Mtree {
int val;
struct Mtree* ltree, * rtree;} mtree;
mtree root1;
void dfs(mtree* rt, int val) {
rt->val = val;
if (val <= m) {
cnt += 1;
rt->ltree = new mtree;
rt->rtree = new mtree;
}
else return;dfs(rt->ltree, val * 2);
dfs(rt->rtree, val * 2 + 1);
}
int main() {
while (scanf("%d%d", &n, &m) != EOF)
{
cnt = 0;
mtree t1;
dfs(&t1, n);
printf("%d\n", cnt);
}
}
登录后发布评论
暂无评论,来抢沙发