题意:

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

trick:

用char输入子结点没有考虑两位数的结点。。。

stoi(x)可以将x转化为十进制整数

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
2 #include<bits/stdc++.h>
3 using namespace std;
4 bool vis[1007];
5 int lchild[27],rchild[27];
6 int num[27];
7 int n;
8 int check(){
9 memset(vis,0,sizeof(vis));
10 for(int i=0;i<n;++i)
11 vis[num[i]]=1;
12 for(int i=1;i<=n;++i)
13 if(!vis[i])
14 return 0;
15 return 1;
16 }
17 int main(){
18 ios::sync_with_stdio(false);
19 cin.tie(NULL);
20 cout.tie(NULL);
21 cin>>n;
22 memset(lchild,-1,sizeof(lchild));
23 memset(rchild,-1,sizeof(rchild));
24 for(int i=0;i<n;++i){
25 string x,y;
26 cin>>x>>y;
27 if(x!="-")
28 lchild[i]=stoi(x),vis[stoi(x)]=1;
29 if(y!="-")
30 rchild[i]=stoi(y),vis[stoi(y)]=1;
31 }
32 int root=0;
33 for(int i=0;i<n;++i)
34 if(!vis[i])
35 root=i;
36 queue<int>q;
37 q.push(root);
38 int last=0;
39 num[root]=1;
40 while(!q.empty()){
41 int now=q.front();
42 last=now;
43 q.pop();
44 if(lchild[now]!=-1){
45 q.push(lchild[now]);
46 num[lchild[now]]=num[now]*2;
47 }
48 if(rchild[now]!=-1){
49 q.push(rchild[now]);
50 num[rchild[now]]=num[now]*2+1;
51 }
52 }
53 if(check()==1)
54 cout<<"YES "<<last;
55 else
56 cout<<"NO "<<root;
57 return 0;
58 }

【PAT甲级】1110 Complete Binary Tree (25分)的更多相关文章

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

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

  2. PAT 甲级 1110 Complete Binary Tree

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

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

  7. 1110. Complete Binary Tree (25)

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

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

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

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

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

随机推荐

  1. Spring Boot整合Freemarker

    一.首先导入依赖 <!-- 添加freemarker模版的依赖 --> <dependency>     <groupId>org.springframework. ...

  2. DataGridView 定位到指定行

    //定位到指定行(样式)dataGridView1.ClearSelection();dataGridView1.Rows[selectIndex].Selected = true; //让指定行处于 ...

  3. Linux安装Tomcat,解决不能访问Manager App

      在Windows环境中安装Tomcat时,只需要在Tomcat目录下/conf/tomcat-user.xml文件增加用户就可以了.在Linux系统中添加了username还是不能访问. 一.Li ...

  4. Session方法

    Session的save()和persist()方法Session的save()方法使一个临时对象转变为持久化对象.它完成以下操作:(1)将临时对象加入到Session缓存中,使其进入持久化状态.(2 ...

  5. C++ 获取当前正在执行的函数的相关信息(转)

    该功能用在日志打印中 原文地址:C++ 获取当前正在执行的函数的相关信息

  6. C++-hihoCode1546-[快速幂]

    枚举子集,要求子集的min+max<=k,求子集个数,答案对1000000007取模 #include <bits/stdc++.h> using namespace std; ,M ...

  7. Django 初试水(一)

    2020年注定是一个不平凡的一年!坚持就是胜利,一起加油! 至于为什么使用 Django,也不想说太多.个人喜欢,这里不做介绍.直接进入主题.show me the code!!! python 的环 ...

  8. centos7安装谷歌浏览器

    1. 安装: 考虑到国内无法访问Google,所以需要自己配置yum源: 1.在目录 /etc/yum.repos.d/ 下新建google-chrome.repo文件,命令如下: 1.cd /ect ...

  9. [NOI2015] 品酒大会 - 后缀数组,并查集,STL,启发式合并

    [NOI2015] 品酒大会 Description 对于每一个 \(i \in [0,n)\) 求有多少对后缀满足 LCP 长度 \(\le i\) ,并求满足条件的两个后缀权值乘积的最大值. So ...

  10. android 获取颜色 getColor 方法 deprecated 过期

    可以使用下面代码代替: ContextCompat.getColor(getContext(), R.color.post_list_content_color) 需要引入: compile 'com ...