题解 | #树查找#

树查找

https://www.nowcoder.com/practice/9a10d5e7d99c45e2a462644d46c428e4

// 树查找
// https://www.nowcoder.com/practice/9a10d5e7d99c45e2a462644d46c428e4
// Hard
#include <iostream>
#include <vector>

using namespace std;

int main(){
    int n;
    vector<pair<int, int> > vp;
    pair<int, int> p;
    int deep;
    while(cin >> n){
        for(int i = 0; i < n; ++i){
            cin >> p.first;
            for(int d = 0; ; d++){
                if(i+1 >= 1 << d && i+1 < 1 << (d+1)){
                    p.second = d+1;
                    break;
                }
            }
            vp.push_back(p);
        }
        cin >> deep;
        while(!vp.empty()){
            if(vp.front().second == deep){
                cout << vp.front().first;
                vp.erase(vp.begin());
                if(vp.front().second == deep) cout << " ";
                else {
                    vp.erase(vp.begin(), vp.end());
                    cout << endl;
                }
            }
            else{
                vp.erase(vp.begin());
                if(vp.empty()) {
                    cout << "EMPTY";
                    cout << endl;
                }
            }
        }
    }
    return 0;
}

根据树的特征来解

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务