A1110. 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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
typedef struct{
int lchild, rchild;
}node;
node tree[];
int str2num(char ss[]){
int len = strlen(ss);
if(ss[] == '-')
return -;
int P = , ans = ;
for(int i = len - ; i >= ; i--){
ans += (ss[i] - '') * P;
P *= ;
}
return ans;
}
int N, lastNode, root, notRoot[] = {}, cnt = , tag = ;
void levelOrder(int root){
queue<int> Q;
Q.push(root);
while(Q.empty() == false){
int temp = Q.front();
lastNode = temp;
Q.pop();
cnt++;
if(cnt < N / && (tree[temp].lchild == - || tree[temp].rchild == -)){
tag = ;
return;
}else if(cnt == N / && (tree[temp].lchild == - && tree[temp].rchild == - || tree[temp].lchild == - && tree[temp].rchild != -)){
tag = ;
return;
}else if(cnt > N / && (tree[temp].lchild != - || tree[temp].rchild != -)){
tag = ;
return;
}
if(tree[temp].lchild != -)
Q.push(tree[temp].lchild);
if(tree[temp].rchild != -)
Q.push(tree[temp].rchild);
}
}
int main(){
scanf("%d", &N);
char str[];
for(int i = ; i < N; i++){
scanf("%s", str);
tree[i].lchild = str2num(str);
scanf("%s", str);
tree[i].rchild = str2num(str);
if(tree[i].lchild != -)
notRoot[tree[i].lchild] = ;
if(tree[i].rchild != -)
notRoot[tree[i].rchild] = ;
}
for(int i = ; i < N; i++){
if(notRoot[i] == ){
root = i;
break;
}
}
levelOrder(root);
if(tag == )
printf("NO %d", root);
else printf("YES %d", lastNode);
cin >> N;
return ;
}
总结:
1、题目要求判断二叉树是否是完全二叉树。我的办法是二叉树的层序遍历,访问一个节点就cnt++。访问前 N/2 - 1个节点时要求必须都有左右孩子。第N/2个节点要求必须是左右都非空或左非空右为空。N/2之后的节点要求左右子树都必须空。
2、网上看到还有更简单的方法,就是在层序遍历的时候把为-1的空节点也都加入队列。当访问时,遇到-1节点则查看cnt,如果cnt<N 则说明不是完全二叉树。另外注意,最后一个非空节点不一定是N-1。因为虽然是完全二叉树,但它的层次遍历节点序号不是按照0、1、2、3的顺序。
A1110. Complete Binary Tree的更多相关文章
- PAT A1110 Complete Binary Tree (25 分)——完全二叉树,字符串转数字
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- PAT甲级——A1110 Complete Binary Tree【25】
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- PAT_A1110#Complete Binary Tree
Source: PAT A1110 Complete Binary Tree (25 分) Description: Given a tree, you are supposed to tell if ...
- 1110 Complete Binary Tree (25 分)
1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...
- PAT1110:Complete Binary Tree
1110. Complete Binary Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter
A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...
- PAT 甲级 1110 Complete Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...
- [二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)
1110. Complete Binary Tree (25) Given a tree, you are supposed to tell if it is a complete binary tr ...
- PAT 1110 Complete Binary Tree[判断完全二叉树]
1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...
随机推荐
- js中this指向、箭头函数
普通函数:this指向分为4种情况,1. obj.getName();//指向obj2.getName();//非严格模式下,指向window,严格模式下为undefined3. var a = ne ...
- WPF 如何创建自己的WPF自定义控件库
在我们平时的项目中,我们经常需要一套自己的自定义控件库,这个特别是在Prism这种框架下面进行开发的时候,每个人都使用一套统一的控件,这样才不会每个人由于界面不统一而造成的整个软件系统千差万别,所以我 ...
- python数据结构与算法第四天【代码执行时间测试模块】
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ from timeit import Timer def foo(): ''' 使用append方式向列表添加 ...
- DOSD用scratch的方式训练通用目标检测,性能很高
推荐一篇今年ICCV上基于DenseNet的general object detection的工作.这是目前已知的第一篇在完全脱离ImageNet pre-train模型的情况下使用deep mode ...
- CUDA开发
CUB库 https://nvlabs.github.io/cub/index.html
- 11/5/2018模拟 Problem C
题面 题解 我有特殊的哈希技巧 以到下一个相同字符的距离为值哈希, 如果不存在或在串外, 就是 \(|T| + 1\). 加入一个新字符 \(S_i\) 时, 同时修改它上一次出现时的值, 由 \(| ...
- JarvisOJ Basic Base64?
GUYDIMZVGQ2DMN3CGRQTONJXGM3TINLGG42DGMZXGM3TINLGGY4DGNBXGYZTGNLGGY3DGNBWMU3WI=== 题目非常具有迷惑性,我一开始以为就是一 ...
- PlaNet,使用图像输入来学习世界模型
Google AI团队与DeepMind合作,上周宣布了一个名为PlaNet的新的开源“Deep Planning”网络. PlaNet是一个人工智能代理,它只使用图像输入来学习世界模型,并使用这些模 ...
- Codeforces Round #488 Div. 1
A:枚举每个点判断是否同时在两个正方形中即可. #include<iostream> #include<cstdio> #include<cmath> #inclu ...
- Edge Deletion CodeForces - 1076D(水最短路)
题意: 设从1到每个点的最短距离为d,求删除几条边后仍然使1到每个点的距离为d,使得剩下的边最多为k 解析: 先求来一遍spfa,然后bfs遍历每条路,如果d[v] == d[u] + Node[i] ...