【PAT甲级 - C++题解】1110 Complete Binary Tree

简介: 【PAT甲级 - C++题解】1110 Complete Binary Tree

1110 Complete Binary Tree

Given a tree, you are supposed to tell if it is a complete binary tree.


Input Specification:


Each input file contains one test case. For each case, the first line gives a positive integer N (≤20) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.

Output Specification:


For each case, print in one line YES and the index of the last node if the tree is a complete binary tree, or NO and the index of the root if not. There must be exactly one space separating the word and the number.

Sample Input 1:

9
7 8
- -
- -
- -
0 1
2 3
4 5
- -
- -


Sample Output 1:

YES 8
• 1

Sample Input 2:

8
- -
4 5
0 6
- -
2 3
- 7
- -
- -

Sample Output 2:

NO 1
• 1

题意


给定一棵二叉树,第一行给定 N NN ,表示有 N NN 个结点,且结点编号为 0 ∼ N − 1 0\sim N-10∼N−1 。


接下来 N NN 行输入 0 ∼ N − 1 0\sim N-10∼N−1 个结点的左右孩子编号,空结点用 - 表示。


判断该二叉树是否是完全二叉树,如果是则输出 YES 和完全二叉树的最后一个结点编号,否则输出 NO 和该树的根结点编号。

思路


我们可以利用完全二叉树的性质,用一个一维数组来存储,如果当前结点第下标为 k (假设下标从 1 开始),则满足以下条件:

该结点的左孩子下标为 k * 2

该结点的右孩子下标为 k * 2 + 1

所以如果一个二叉树是完全二叉树的话,将它所有结点按上述方式存储到一维数组中是可以刚好填满 1 ∼ N 1\sim N1∼N(下标从 1 11 开始)个位置的。相反如果不是完全二叉树,则 1 ∼ N 1\sim N1∼N 个位置中会有空余,也就是说最后一个结点的位置会大于 N NN 。故我们只用判断最后一个结点的位置是否为 N NN 即可,如果是 N NN 则说明是完全二叉树。

最后输出判断结果,注意如果是完全二叉树还需要输出最后一个结点的编号,否则输出该树的根结点编号。

我们拿题目的第一个样例举例,可以得到下面这颗完全二叉树:



6cf7c17f22d64cb097f7b9ba7974e890.png



其在一维数组中的存储情况为:

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 25;
int l[N], r[N], has_parent[N];
int n, max_k, max_id;
void dfs(int u, int k)
{
    if (u == -1)   return;
    //如果当前遍历到的下标k更大,则进行更新
    if (k > max_k)
    {
        max_k = k;    //找到最后一个结点在一维数组中的下标
        max_id = u;   //找到最后一个结点的编号
    }
    dfs(l[u], k * 2);
    dfs(r[u], k * 2 + 1);
}
int main()
{
    //初始化
    cin >> n;
    memset(l, -1, sizeof l);
    memset(r, -1, sizeof r);
    //输入结点信息
    for (int i = 0; i < n; i++)
    {
        string a, b;
        cin >> a >> b;
        if (a != "-")  l[i] = stoi(a), has_parent[l[i]] = true;
        if (b != "-")  r[i] = stoi(b), has_parent[r[i]] = true;
    }
    //找到根结点下标
    int root = 0;
    while (has_parent[root]) root++;
    dfs(root, 1);
    if (max_k == n)    printf("YES %d\n", max_id);
    else    printf("NO %d\n", root);
    return 0;
}


目录
相关文章
|
4天前
|
JSON 数据格式 C++
C++ JSON库 nlohmann::basic_json::binary 的用法
C++ JSON库 nlohmann::basic_json::binary 的用法
31 0
|
11月前
|
C++
【PAT甲级 - C++题解】1040 Longest Symmetric String
【PAT甲级 - C++题解】1040 Longest Symmetric String
36 0
|
11月前
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
57 0
|
11月前
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
51 0
|
4天前
|
设计模式 安全 算法
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
【C++入门到精通】特殊类的设计 | 单例模式 [ C++入门 ]
18 0
|
4天前
|
C语言 C++
【C++】string类(常用接口)
【C++】string类(常用接口)
21 1
|
2天前
|
测试技术 C++
C++|运算符重载(3)|日期类的计算
C++|运算符重载(3)|日期类的计算
|
4天前
|
C语言 C++ 容器
C++ string类
C++ string类
9 0
|
4天前
|
C++ Linux