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. 360p以上

    请选择200MB以内,360p以上,时长3秒到300秒的文件.(支持格式:mp4.3gp.avi.mov.m4v.mkv文件)

  2. 【Bzoj2260】【Bzoj4349】商店购物 & 最小树形图

    目录 List Bzoj 2260 商店购物 Description Input Output Sample Input Sample Output Bzoj 4349 最小树形图 Descripti ...

  3. [JSOI 2012] 玄武密码

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4327 [算法] AC自动机[代码] #include<bits/stdc++. ...

  4. 02、监听key是否正确

    SDKInitializer其实是有两个action,第三个SDK_BROADTCAST_INTENT_EXTRA_INFO_KEY_ERROR_CODE虽然是一个常量但是它不是action.第二个a ...

  5. gulp的使用安装

    gulp安装:用cnpm的时候把npm换成cnpm就好了. npm install -g gulp(全局安装,安装一次就好) npm install --save-dev gulp(安装到项目目录下, ...

  6. codevs1574广义斐波那契数列

    1574 广义斐波那契数列  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond     题目描述 Description 广义的斐波那契数列是指形如an=p* ...

  7. 在chrome里模拟调试微信浏览器

    开发者模式(下面有配图): 开发者模式/DevTools.More tools/Network conditions/User agent/ Custom/安卓或ios代理配置配置 更改User ag ...

  8. POJ 1873 计算几何

    思路: 二进制枚举一下要删哪些点 求个凸包,算一下贡献 //By SiriusRen #include <cmath> #include <cstdio> #include & ...

  9. [转]Android杂谈--ListView之BaseAdapter的使用

    本文转自:http://blog.csdn.net/tianshuguang/article/details/7344315 话说开发用了各种Adapter之后感觉用的最舒服的还是BaseAdapte ...

  10. C语言常量

    Constant包括4种类型: 整型 浮点型 枚举 字符型 #include <stddef.h> #include <uchar.h> int main() { /* Int ...