PAT A1110 Complete Binary Tree (25 分)——完全二叉树,字符串转数字
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 <stdio.h>
#include <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
const int maxn = ;
int n,k;
struct node{
int l=-,r=-;
}tree[maxn];
int vis[maxn]={};
int main(){
scanf("%d",&n);
getchar();
for(int i=;i<n;i++){
string c1,c2;
cin>>c1>>c2;
getchar();
int n1=-,n2=-;
if(c1!="-"){
n1=stoi(c1);
vis[n1]=;
}
if(c2!="-"){
n2=stoi(c2);
vis[n2]=;
}
tree[i].l=n1;
tree[i].r=n2;
}
int root=-;
for(int i=;i<n;i++){
if(vis[i]==){
root=i;
break;
}
}
int flag=,flag2=,now;
queue<int> q;
q.push(root);
while(!q.empty()){
now=q.front();
q.pop();
//printf("%d ",now);
if(tree[now].l!=-){
if(flag==)q.push(tree[now].l);
else {
flag2=;
break;
}
}
else{
flag=;
}
if(tree[now].r!=-){
if(flag==)q.push(tree[now].r);
else {
flag2=;
break;
}
}
else{
flag=;
}
/*if(now!=-1){
flag++;
flag2=now;
}
else{
if(flag==n){
printf("YES %d",flag2);
}
else{
printf("NO %d",root);
}
return 0;
}
q.push(tree[now].l);
q.push(tree[now].r);
*/
}
if(flag2==)printf("NO %d",root);
else printf("YES %d",now);
}
注意点:完全二叉树的判断就是看已经有节点没孩子了,后面节点却还有孩子,这树就不是完全二叉树。有三个点一直错误,一开始段错误,后来在结构体初始化了-1后答案错误,总找不到原因。后来才发现原来是读输入的时候错了,两位数就读不到了,不能用%c,要用string
PAT A1110 Complete Binary Tree (25 分)——完全二叉树,字符串转数字的更多相关文章
- 1110 Complete Binary Tree (25 分)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- [二叉树建树&完全二叉树判断] 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甲级——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 1110 Complete Binary Tree[判断完全二叉树]
1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...
- PAT Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]
题目 Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each ...
- PAT 1110 Complete Binary Tree[比较]
1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...
- A1110. Complete Binary Tree
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- 1110. Complete Binary Tree (25)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
随机推荐
- Java基础——Servlet(八)文件上传下载
一.简单的文件上传常见的组件Smartupload , Apache 的 commons FileUploadSmartupload上传的步骤: 1.初始化上传上下文 2.准备上传 3.保存文件 &l ...
- Contest2075 - 湖南多校对抗(csu1576)大数 Catalan Square
Problem C: Catalan Square Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 42 Solved: 16[Submit][Stat ...
- System.arraycopy 怎么使用的?
前言:看 ArrayList 的源码,发现 remove 方法主要依赖了 System.arraycopy() 方法实现的.所以需要了解一下这个方法如何使用.转载请注明出处:https://www.c ...
- wcf json参数返回失败问题
问题: 最近写了一个接口,提示连接失败,于是在本地发布了一下,然后模拟post请求进行本地调试,发现能正常进入接口,中间也没问题,一直走到最后一步return时,也能return,但是就是返回不了数据 ...
- Python 练习:三级菜单选择城市
info = { 'GuangDong':{ 'GuangZhou': ['TianHe', 'HaiZhu'], 'MaoMing': ['MaoNan', 'DianBai']}, 'ShanDo ...
- a标签禁止跳转或者不跳转的几种实现方式
1.onclick事件中返回false <a href="http://www.baidu.com" onclick="return false" &g ...
- python中收集函数的解包问题
收集参数的解包问题 - 把参数放入list或者字典中,直接把list/dict中的值放入收集参数中- 语法:参照案例 # 收集参数的问题 def stu(*args): print("=&q ...
- 【读书笔记】iOS-解析XML
使用最广泛的解析XML文档的方法有两种,一种基于SAX,另一种基于DOM.SAX解析器是事件驱动型的,在解析时增量地读取XML文档,当解析器识别出一个结点的时候会调用相应的委托方法. 参考资料< ...
- 安卓开发----TextView控件属性列表(转)
文章原地址: http://wwzcraig.blog.163.com/blog/static/64622969201373184343118/ android:autoLink设置是否当文本为URL ...
- Android architecture