L3-010 是否完全二叉搜索树

#include <bits/stdc++.h>

using namespace std;

const int N = 100;

int tr[N];

int main() {
    int n;
    cin >> n;
    memset(tr, -1, sizeof(tr));
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        int root = 1;
        while (tr[root] != -1) {
            if (x > tr[root]) {
                root *= 2;
            } else {
                root = root * 2 + 1;
            }
        }
        tr[root] = x;
    }

    int mx = 0;
    bool ok = 1;
    for (int i = 1; i <= 99; i++) {
        if (tr[i] != -1 && i == mx + 1) mx++;
        else {
            ok = false;
            break;
        }
    }
    //cout << "mx ==" << mx << endl;
    cout << tr[1];
    for (int i = 2; i <= 99; i++) {
        if (tr[i] != -1) {
            cout << " " << tr[i];
        }
    }
    cout << "\n";
    if (ok || mx == n) cout << "YES" << "\n";
    else cout << "NO" << "\n";

    return 0;
}
posted @ 2022-04-21 10:22  Xxaj5  阅读(16)  评论(0编辑  收藏  举报