【PAT】A1110 Complete Binary Tree (25)

本文介绍了一种用于判断给定树是否为完全二叉树的算法。通过深度优先搜索,算法确定树的最大叶子节点位置,进而判断树的类型。文章详细解释了输入输出规格,并提供了代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1110 Complete Binary Tree (25 point(s))

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

Sample Input 2:

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

Sample Output 2:

NO 1

Code

#include <iostream>
//#include <fstream>
using namespace std;
struct node{
    int l, r;
}nodes[21];							// N <= 20
int maxindex = -1, res;				// maxindex:最大的叶子位置;res:存放根节点序号
void dfs(int root, int index) {		// 深度优先搜索,找到最大的index,找到树的最后一个节点
    if(index > maxindex) {
        maxindex = index;
        res = root;
    }
    if(nodes[root].l != -1) dfs(nodes[root].l, index * 2);
    if(nodes[root].r != -1) dfs(nodes[root].r, index * 2 + 1);
}
int main() {
    //ifstream cin("/home/my/Desktop/in.txt");
    int n, root = 0, haveroot[100] = {0};	// haveroot[],是否有父节点
    cin >> n;
    string l, r;
    for (int i = 0; i < n; i++) {			// 接收输入,存放节点的左右子树
        cin >> l >> r;
        if (l == "-") {
            nodes[i].l = -1;
        } else {
            nodes[i].l = stoi(l);
            haveroot[stoi(l)] = 1;
        }
        if (r == "-") {
            nodes[i].r = -1;
        } else {
            nodes[i].r = stoi(r);
            haveroot[stoi(r)] = 1;
        }
    }
    while (haveroot[root] != 0) root++;	// 找到树的根节点
    dfs(root, 1);
    if (maxindex == n)      printf("YES %d", res);
    else                    printf("NO %d", root);
    return 0;
}
<think>好的,我需要回答用户关于完全二叉树(complete binary tree)的定义的问题。首先,我得回忆一下完全二叉树的基本概念,确保自己理解正确。根据已有的知识,完全二叉树是一种特殊的二叉树结构,它的节点排列需要满足一定的条件。 接下来,我要检查用户提供的引用资料,看看是否有相关的信息可以支持我的回答。用户提到了引用[2]和引用[4],其中引用[2]提到完全二叉树的特点是层次遍历没有中断点,可以用数组实现。而引用[4]则强调不要将满二叉树与完全二叉树混淆,说明两者是不同的概念。 我需要明确完全二叉树的定义,可能包括以下几点:完全二叉树除了最后一层外,其他层都是满的,并且最后一层的节点都尽可能靠左排列。这与满二叉树不同,满二叉树所有层都是满的。同时,完全二叉树可以用数组高效存储,因为节点之间没有空隙。 然后,我需要组织语言,用中文清晰表达这一定义,并引用相关的资料来支持。例如,引用[2]提到完全二叉树的层次遍历没有中断点,适合数组实现,这可以作为一个引用点。引用[4]则用来区分完全二叉树和其他类型的树,如满二叉树。 另外,用户要求生成相关问题,我需要根据完全二叉树的定义和应用,提出几个相关的问题,比如与满二叉树的区别、应用场景、数组实现方式等。 最后,确保回答结构清晰,使用正确的LaTeX格式,如变量用$...$,独立公式用$$...$$,并且检查引用标注是否自然添加在段落末尾。避免使用任何Markdown格式,保持回答简洁明了。</think>### 完全二叉树(Complete Binary Tree)的定义 完全二叉树是一种满足以下性质的二叉树: 1. **除最后一层外,其他层节点数达到最大值**,即每层都是满的; 2. **最后一层的节点必须从左到右连续排列**,不能有空缺[^2][^4]。 例如,若一棵完全二叉树的深度为$h$,则前$h-1$层的节点构成满二叉树,第$h$层的节点依次填充左侧位置。这种特性使得完全二叉树可以用数组高效存储,因为其层次遍历结果与数组顺序一致,且无“空洞”[^2]。 #### 与满二叉树的区别 - **满二叉树**:所有层的节点数均达到最大值,即深度为$k$的满二叉树有$2^k - 1$个节点; - **完全二叉树**:仅要求最后一层左侧连续填充,其他层满,节点数可能少于满二叉树。 #### 应用场景 完全二叉树常用于堆(Heap)的实现(如优先队列),或需要数组存储的高效树结构[^2]。 ```python # 完全二叉树的数组表示示例 tree = [1, 2, 3, 4, 5, 6, None] # 最后一层左侧连续,右侧允许None ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值