题解 | 树查找
树查找
https://www.nowcoder.com/practice/9a10d5e7d99c45e2a462644d46c428e4
#include <iostream>
#include<cstring>
using namespace std;
const int null = 0x3f3f3f3f;
const int N = 1010;
int e[N];
int d;
bool isempty = true;
void preorder(int i, int h) {
if (e[i] == null)return;
if (h == d) {
isempty = false;
cout << e[i] << ' ';
}
preorder(2 * i, h + 1);
preorder(2 * i + 1, h + 1);
}
int main() {
int n;
while (cin >> n ) { // 注意 while 处理多个 case
memset(e, 0x3f, sizeof e);
isempty = true;
for (int i = 1; i <= n; i++)cin >> e[i];
cin >> d;
preorder(1, 1);
if (isempty)cout << "EMPTY" << endl;
cout << endl;
}
}
// 64 位输出请用 printf("%lld")
查看19道真题和解析