题解 | 树查找

树查找

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")

全部评论

相关推荐

05-12 13:14
已编辑
中山大学 算法工程师
点赞 评论 收藏
分享
风的叶脉:不知道但我想要鞭打你( '-' )ノ)`-' ) 加油
点赞 评论 收藏
分享
04-18 00:32
已编辑
中南大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务