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 ...
随机推荐
- FireFox升级后FireBug不能使用
今天发现,火狐浏览器从49.0.2升级到50.0.2之后,firebug的js调试被禁用了,果断去找49.0.2的版本. 链接: https://ftp.mozilla.org/pub/firefox ...
- php中一个字符占用几个字节?
先看看字符与字节有什么区别: (一)“字节”的定义 字节(Byte)是一种计量单位,表示数据量多少,它是计算机信息技术用于计量存储容量的一种计量单位. (二)“字符”的定义 字符是指计算机中使用的文字 ...
- Linux常用系统命令
致歉:各位看到此博客的朋友们 因为命令的数量挺多的很多命令也都很简单 我就总结了一下具体的命令和这个命令是做什么的,主要的使用方法是链接到http://man.linuxde.net/的网站的,请各 ...
- Jenkins报错'Gradle build daemon disappeared unexpectedly'的问题解决
在将项目集成到 Jenkins 后,经常会出现不稳定的构建,Jenkins 控制台输出的错误信息为:Gradle build daemon disappeared unexpectedly (it m ...
- Java并发编程(十二)Callable、Future和FutureTask
一.Callable与Runnable 先说一下java.lang.Runnable吧,它是一个接口,在它里面只声明了一个run()方法: public interface Runnable { pu ...
- 监听软件异常崩溃并且保持日志--CrashHandler编写自己的异常捕获类
平时写代码,我们可能会抛出各种异常,这些异常有些是我们测试过程中发现进行解决的,但是也有一些异常是我们未知的,不论是代码的逻辑问题还是Android本身底层的一些bug,我们都需要及时了解并进行解决. ...
- JS获取当前星期几的简易写法
var str = "今天是星期" + "日一二三四五六".charAt(new Date().getDay()); mark在此,方便日后复制 原文https ...
- PL/SQL 删除主键 ORA-02443: 无法删除约束条件-不存在的约束条件
在PL/SQL developer中删除一个表的主键,然后把另外一个字段设置成主键,删除的过程中报错:ORA-02443 我遇到这个问题出现的背景是: alter table saleqtya dro ...
- Oracle 表操作(转)
1.增加新字段:alter table table_name add (name varchar(20) default 'http://www.zangjing.net/');. 2.修改表字段:a ...
- PHP Excel导入数据到MySQL数据库
数据导出已经有了,怎么能没有数据导入呢,同样使用TP5框架,首先需要下载phpexcel.zip,放到第三方类库目录vendor目录下,然后有一个页面可以让你选择要导入的Excel文件,然后点击导入按 ...