1110 Complete Binary Tree

本文介绍了一种判断一棵树是否为完全二叉树的方法,通过构建树并给每个节点编号,比较最大编号与节点总数来确定。文章提供了C++实现代码,包括树的结构定义、广度优先搜索算法及主函数。

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

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
Sample Input 2:
8
- -
4 5
0 6
- -
2 3
- 7
- -
- -
Sample Output 2:
NO 1
分析:判断一棵树是否为完全二叉树。
先构建一棵树,然后给树的每个结点进行编号,根结点为1,左孩子结点的编号为2,有孩子结点的编号为3,以此类推,最大编号maxn与节点个数n进行比较,如果两者相等,则此树是完全二叉树,反之则不是。
参考代码(来自别处)

#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct node {
    int left, right;
};
vector<node>tree;
int maxn = 0, ans = 0;
void bfs(int vec,int index) {
    if (index > maxn) {
        maxn= index;
        ans = vec;
    }
    if (tree[vec].left != -1)bfs(tree[vec].left, 2 * index);
    if (tree[vec].right != -1)bfs(tree[vec].right, 2 * index+1);
}
int main()
{
    int n, root=0;
    scanf_s("%d", &n);
    vector<int>v;
    v.resize(n, -1);
    string l, r;
    tree.resize(n);
    getchar();
    for (int i = 0; i < n; i++) {
        cin >> l >> r;
        if (l == "-")tree[i].left = -1;
        else {
            tree[i].left = stoi(l);
            v[stoi(l)] = 1;
        }
        if (r == "-")tree[i].right = -1;
        else {
            tree[i].right = stoi(r);
            v[stoi(r)] = 1;
        }
    }
    while (v[root]!=-1) { root++; }
    bfs(root, 1);
    if (maxn== n)cout << "YES" << " " << ans;
    else cout << "NO" << " " << 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、付费专栏及课程。

余额充值