首页 > 试题广场 >

二叉树

[编程题]二叉树
  • 热度指数:14632 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    如上所示,由正整数1,2,3……组成了一颗特殊二叉树。我们已知这个二叉树的最后一个结点是n。现在的问题是,结点m所在的子树中一共包括多少个结点。     比如,n = 12,m = 3那么上图中的结点13,14,15以及后面的结点都是不存在的,结点m所在子树中包括的结点有3,6,7,12,因此结点m的所在子树中共有4个结点。

输入描述:
    输入数据包括多行,每行给出一组测试数据,包括两个整数m,n (1 <= m <= n <= 1000000000)。


输出描述:
    对于每一组测试数据,输出一行,该行包含一个整数,给出结点m所在子树中包括的结点的数目。
示例1

输入

3 12
0 0

输出

4
头像 大内高手
发表于 2020-03-12 15:46:23
分治:即把一个复杂的问题分成两个或多个子问题,子问题之间相互独立且与原问题相同或相似,持续分解,直到最后的子问题可以简单的求解。原问题即子问题解的合并。 由于反复利用分治手段,这就为使用递归策略提供了条件。 解法:采用递归策略,根据二叉树的性质:左子树编号为2m,右子树为2m+1,不断的去计算节点 展开全文
头像 yyer
发表于 2023-01-09 18:01:11
#include <iostream> using namespace std; int n,m; int f(int m){ if(m>n) return 0; if(2*m>n && 2*m+1>n) return 1; return f(2 展开全文
头像 帅呆呆~
发表于 2022-03-02 18:43:04
">#include<iostream> using namespace std; int CountNodes(int m,int n){ if(m > n){ //递归出口 return 0; } return CountNodes(2 * m,n) + 展开全文
头像 复旦周杰伦
发表于 2023-02-28 14:21:40
#include <iostream> #include "cstdio" int pointNum(int m, int n) { if (2 * m + 1 <= n) return pointNum(2 * m, n) + pointNum(2 * 展开全文
头像 miner_song
发表于 2022-08-02 16:39:13
#include <bits/stdc++.h> using namespace std; int fun(int m,int n) {     int r;     if(m>n)  &n 展开全文
头像 shininggirls01
发表于 2024-03-29 22:57:54
#include <iostream> using namespace std; //完全二叉树左2i右2i+1 int Cal(int m, int n) { if (m > n) return 0; return 1 + Cal(2 * m, n) + Cal 展开全文
头像 牛客387239580号
发表于 2021-02-18 12:08:27
北京大学复试上机题(二叉树)可采用分治的方法,先统计m点的节点数,然后递归地统计m左子树和右子树的节点数,当节点编号大于n时,递归返回。 #include <bits/stdc++.h> using namespace std; int sum = 0; // 欲输出的节 展开全文
头像 MountainsHao
发表于 2024-03-18 17:18:57
#include <stdio.h> int BinaryTree(int m, int n) { if (m <= n) { return 1 + BinaryTree(m * 2, n) + BinaryTree(m * 2 + 1, n); 展开全文
头像 牛客774160366号
发表于 2023-06-13 14:03:32
#include <stdio.h> #include <string.h> #include <stdlib.h> int count(int m, int n) { if (m > n) { return 0; } else if (m 展开全文
头像 这破程序员一秒都不想当了
发表于 2024-03-20 21:12:29
#include <cstdio> #include <iostream> using namespace std; long long counter = 1; void fc(long long a,long long b) // a:target node ; b: 展开全文