1110. Complete Binary Tree (25)

本文介绍了一种通过输入节点信息来判断一棵树是否为完全二叉树的方法,并提供了一个具体的实现示例。

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

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

和前面的题目一样,,没什么变化,注意输入用string不用char。

#include <iostream>
#include <cstring>
#include <map>
using namespace std;
const int maxn = 25;
int n, tree[maxn][2];
map<int, int> bst;
void dfs(int root, int idx)
{
if(root == -1)
return ;
int left = tree[root][0], right = tree[root][1];
dfs(left, 2 * idx + 1);
bst[idx] = root;
dfs(right, 2 * idx + 2);
}
int main()
{
string x, y;
scanf("%d", &n);
int sum = 0;
memset(tree, -1, sizeof(tree));
for(int i = 0; i < n; ++i){
cin>>x>>y;
if(x[0] != '-'){
tree[i][0] = atoi(x.c_str());
sum += atoi(x.c_str());
}
if(y[0] != '-'){
tree[i][1] = atoi(y.c_str());
sum += atoi(y.c_str());
}
}
int root = n * (n - 1) / 2 - sum;
dfs(root, 0);
int i;
for(i = 0; i < n; ++i){
if(bst.count(i) == 0){
printf("NO %d", root);
return 0;
}
}
if(i == n)
printf("YES %d", bst[i -  1]);
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、付费专栏及课程。

余额充值