题解 | #二叉树#
二叉树
https://www.nowcoder.com/practice/f74c7506538b44399f2849eba2f050b5
#include <iostream>
#include <string>
#include <queue>
using namespace std;
int DFS(int index, int end) {
if (index > end) {
return 0;
} else {
int left_child = index * 2;
int right_child = index * 2 + 1;
return 1 + DFS(left_child, end) + DFS(right_child, end);
}
}
int main() {
int M, N;
while (scanf("%d %d", &M, &N) != EOF) {
if (M == 0) {
break;
}
cout << DFS(M, N);
}
}
查看15道真题和解析