其他分享
首页 > 其他分享> > L3-010 是否完全二叉搜索树

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;
}

标签:int,namespace,memset,tr,二叉,010,L3,root
来源: https://www.cnblogs.com/ZhengLijie/p/16173020.html