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<iostream>
#include<vector>
#include<string>
using namespace std;
vector<int> tree(21,-1), Left(20,-1), Right(20,-1), record(20,0);
int n;
void buildtree(int root){
if(Left[tree[root]]!=-1){
if(2*root+1>19)
return;
tree[2*root+1]=Left[tree[root]];
buildtree(2*root+1);
}
if(Right[tree[root]]!=-1){
if(2*root+2>19)
return;
tree[2*root+2]=Right[tree[root]];
buildtree(2*root+2);
}
}
int main(){
cin>>n;
string l, r;
for(int i=0; i<n; i++){
cin>>l>>r;
if(l[0]!='-'){
Left[i]=stoi(l);
record[Left[i]]=1;
}
if(r[0]!='-'){
Right[i]=stoi(r);
record[Right[i]]=1;
}
}
int i=0;
for(; i<n; i++)
if(record[i]==0)
break;
tree[0]=i;
buildtree(0);
int cnt=0;
for(int i=0; i<=20; i++)
if(tree[i]==-1)
if(i==n){
cout<<"YES "<<tree[n-1]<<endl;
return 0;
}
else{
cout<<"NO "<<tree[0]<<endl;
return 0;
}
}

PAT 1110 Complete Binary Tree的更多相关文章

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

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

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

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

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

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

  6. 1110 Complete Binary Tree

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

  7. PAT 甲级 1110 Complete Binary Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...

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

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

随机推荐

  1. cacheed 限制 4节点 3000万 es 批量删除 shell脚本练习 elasticsearch_action

    文件分割 "www.laiwunews.cn/xinxi/25324717.html""www.zznews.cn/xinxi/10411214.html"&q ...

  2. java多线程——饥饿和公平

    一.概念 饥饿:如果一个线程因为 CPU 时间全部被其他线程抢走而得不到 CPU 运行时间,这种状态被称之为“饥饿”: 二.饥饿原因 高优先级线程吞噬所有的低优先级线程的 CPU 时间. 线程被永久堵 ...

  3. Excel 常用快捷键键 快捷方式

    移动整行的位置 Shift + Alt + 鼠标拖拽 不加Shit + Alt 移动后 留白 注意:需要移动鼠标到行的最上面,变成十字箭头

  4. C# 获得资源文件下图片的路径

    最终实现正确的代码是: button8.Image = System.Drawing.Image.FromFile(@"..\\..\\Resources\\GAOJIBAN.png&quo ...

  5. PCB SVN 服务端VisualSVN Server与TortoiseSVN

    PCB 工程系统SVN源代码招病毒破坏以后,一周时间都没有源代码同步更新了,今天终于将SVN源代码数据恢复并重建SVN服务器,这里将SVN搭建安装过程整理如下 一.服务端SVN安装 1.下载地址:ht ...

  6. P3128 [USACO15DEC]最大流Max Flow(LCA+树上差分)

    P3128 [USACO15DEC]最大流Max Flow 题目描述 Farmer John has installed a new system of  pipes to transport mil ...

  7. [Swift通天遁地]七、数据与安全-(18)使用Swift实现原生的MD5加密

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. Java统计一个字符串中各个字符出现的次数

    相信很多人在工作的时候都会遇到这样一个,如何统计一个字符串中各个字符出现的次数呢,这种需求一把用在数据分析方面,比如根据特定的条件去查找某个字符出现的次数.那么如何实现呢,其实也很简单,下面我贴上代码 ...

  9. composer 下载安装

    linux/mac os curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/compos ...

  10. SCOI2014题解

    SCOI2014由极不靠谱的电子坑爹大学出题.电子坑爹大学打破了多年行规,暴力+NOIP500分居然不能进队.极其逗比的第一天暴力给10分!!还好有些题不是很难,省队爷分数大概在150以上(最高420 ...