PAT甲级真题1110 完全二叉树

题面

在这里插入图片描述

题解

在这里插入图片描述

我们用两个数组 l,r 分别存储 第 i 号根节点的左右儿子 ,在读取的时候标记其有没有父节点,然后就可以找出其父节点。

然后我们可以从根节点开始遍历,如果是完全二叉树,那么最后一个节点的编号一定等于节点数,可以看图中蓝色部分,我们只需要用一个maxn记录dfs过程中最大值,最后判断一下与n的关系即可

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>

using namespace std;
const int N = 25;

int n;
int l[N], r[N]; //i号节点的左右儿子
bool is_father[N];  //有没有父亲节点
int maxn, last;//最后一个节点的值

void dfs(int root, int k) {
    
    
    if (maxn < k) {
    
    
        maxn = k;
        last = root;
    }
    if (l[root] != -1) dfs(l[root], k * 2);
    if (r[root] != -1) dfs(r[root], k * 2 + 1);
}

int main() {
    
    

    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    memset(l, -1, sizeof l);
    memset(r, -1, sizeof r);

    cin >> n;
    for (int i = 0; i < n; i++) {
    
    
        string a, b;
        cin >> a >> b;
        if (a != "-") {
    
    
            l[i] = stoi(a);  //转换成数字
            is_father[stoi(a)] = true;
        }
        if (b != "-") {
    
    
            r[i] = stoi(b);
            is_father[stoi(b)] = true;
        }
    }

    //求根节点
    int root = 0;
    while (is_father[root]) root++;

    dfs(root, 1);

    if (maxn > n) {
    
    
        cout << "NO " << root << endl;

    } else {
    
    
        cout << "YES " << last << endl;

    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44791484/article/details/115113579