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 (≤) 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
/*
思路是先建树,找根节点,先序遍历找到最大节点,如果等于n表示完全二叉树
*/
#include<iostream>
#include<cstdlib>
using namespace std;
const int maxn = ; bool isRoot[maxn] = {};
int max_index = -,last_v;
int root = ; struct Node
{
int left,right;
}node[maxn]; int getNum(string s)
{
int iRet = -;
if(s != "-")
{
iRet = atoi(s.c_str());
isRoot[iRet] = ;
}
return iRet;
} void FindRoot(int n)
{
for(int i = ; i < n; i++)
{
if( == isRoot[i])
{
root = i;
break;
}
}
} void preOrder(int v,int index)
{
if(- == v)
{
return;
}
if(index > max_index)
{
max_index = index;
last_v = v;
}
preOrder(node[v].left,index*);
preOrder(node[v].right,index*+);
} int main()
{
int n;
cin >> n;
string s1,s2;
for(int i = ; i < n; i++)
{
cin >> s1 >> s2;
node[i].left = getNum(s1);
node[i].right = getNum(s2);
}
FindRoot(n);
preOrder(root,);
if(max_index == n)
{
printf("YES %d",last_v);
}
else
{
printf("NO %d",root);
}
return ;
}

下面版本三个点段错误,待查

#include<cstdio>

const int maxn = ;
struct Node
{
int left,right;
}node[maxn]; int max_index = -,last_v;
bool isRoot[maxn] = {};
int root; int changeNum(char c)
{
int iRet = -;
if(c != '-')
{
iRet = c - '';
isRoot[iRet] = ;
}
return iRet;
} void FindRoot(int n)
{
for(int i = ; i < n; i++)
{
if( == isRoot[i])
{
root = i;
break;
}
}
} void preOrder(int v,int index)
{
if(- == v)
{
return;
}
if(index > max_index)
{
max_index = index;
last_v = v;
}
preOrder(node[v].left,index*);
preOrder(node[v].right,index*+);
} int main()
{
int n;
scanf("%d",&n);
char c1,c2;
for(int i = ; i < n; i++)
{
getchar();
scanf("%c%*c%c",&c1,&c2);
node[i].left = changeNum(c1);
node[i].right = changeNum(c2);
}
FindRoot(n);
preOrder(root,);
if(max_index == n)
{
printf("YES %d",last_v);
}
else
{
printf("NO %d",root);
}
return ;
}

1110 Complete Binary Tree (25 分)的更多相关文章

  1. 【PAT甲级】1110 Complete Binary Tree (25分)

    题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...

  2. [二叉树建树&完全二叉树判断] 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 ...

  3. 1110. Complete Binary Tree (25)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  4. 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 ...

  5. PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

    题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...

  6. PAT (Advanced Level) 1110. Complete Binary Tree (25)

    判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...

  7. PAT甲级——1110 Complete Binary Tree (完全二叉树)

    此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830   1110 Complete Binary ...

  8. 1110 Complete Binary Tree

    1110 Complete Binary Tree (25)(25 分) Given a tree, you are supposed to tell if it is a complete bina ...

  9. 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 ...

随机推荐

  1. sql server查询数据库连接数

    设置最大连接数 下面的T-SQL 语句可以配置SQL Server 允许的并发用户连接的最大数目. exec sp_configure 'show advanced options', 1exec s ...

  2. CNN中feature map、卷积核、卷积核的个数、filter、channel的概念解释

    CNN中feature map.卷积核.卷积核的个数.filter.channel的概念解释 参考链接: https://blog.csdn.net/xys430381_1/article/detai ...

  3. Codeforces 1221F. Choose a Square

    传送门 对于某个点 $(x,y)$ ,不妨设 $x<y$ 因为如果 $x>y$ 直接按 $y=x$ 对称一下即可 当且仅当正方形左下角 $(a,a)$ 满足 $a<=x$,右上角 $ ...

  4. ArrayList扩容分析

    一段java代码 String e = "q3234v"; List<String> list = new ArrayList<String>(); for ...

  5. js中setTimeout和setInterval的应用方法(转)

    JS里设定延时: 使用SetInterval和设定延时函数setTimeout 很类似.setTimeout 运用在延迟一段时间,再进行某项操作. setTimeout("function& ...

  6. 分布式的几件小事(五)dubbo的spi思想是什么

    1.什么是SPI机制 SPI 全称为 Service Provider Interface,是一种服务发现机制. SPI 的本质是将接口实现类的全限定名配置在文件中,并由服务加载器读取配置文件,加载实 ...

  7. 关于redis的几件小事(一)redis的使用目的与问题

    1.redis是用来干嘛的? Redis is an open source (BSD licensed), in-memory data structure store, used as a dat ...

  8. python time,calendar,datetime

    time sleep:休眠指定的秒数(可以是小数) localtime:将一个时间戳转换为time.struct_time类型的对象(类似于元组) # 将一个时间戳转换为一个类似于元组的对象,不指定时 ...

  9. new Date,Date.parse()传值

    获取时间: 获取1997年10月1号日期 new Date: 当使用 - 拼接年月日时将会使用UTC时区解析参数,会比北京时间快八小时. 当时用 / 拼接年月日时会使用北京的时区去解析参数,取到的是北 ...

  10. js 条件方法、数组方法

    经常写代码写的很多很累赘,看看下面例子,争取以后代码简洁简化.个人也觉得简洁分明的代码很重要. 本文来自另一篇博客:https://www.cnblogs.com/ljx20180807/p/1084 ...