题意:

输入一个正整数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. ubuntu19.04 redis启动和停止及连接

    1.启动停止 如果以(sudo apt install redis-server)方式安装 启动: sudo srevice redis start 停止:     sudo srevice redi ...

  2. linux分区命令parted的用法

    parted的适用场景 创建操作大于2T的分区 一般情况下,我们都是选择使用fdisk工具来进行分区,但是目前在实际生产环境中使用的磁盘空间越来越大,呈TiB级别增长:而常用的fdisk这个工具对分区 ...

  3. Python爬虫连载4-Error模块、Useragent详解

    一.error 1.URLError产生的原因:(1)没有网络:(2)服务器连接失败:(3)不知道指定服务器:(4)是OSError的子类 from urllib import request,err ...

  4. 风变编程笔记(二)-Python爬虫精进

    第0关  认识爬虫 1. 浏览器的工作原理首先,我们在浏览器输入网址(也可以叫URL),然后浏览器向服务器传达了我们想访问某个网页的需求,这个过程就叫做[请求]紧接着,服务器把你想要的网站数据发送给浏 ...

  5. HTML页面学习

    HTML 文档结构 <!DOCTYPE html> 文档声明 <html lang="en"> 语言 <head> 网站配置信息 <met ...

  6. HTML表单提交标签

    <form>表单提交标签,设置提交范围 有name属性才能被提交 action:提交的地址url method:提交方式 get方式(默认):会将参数拼接在连接后,有大小限制(4k) po ...

  7. Ubuntu下vsc+python3配置

    我发现直接搜这个很容易得到过时的文章py2请滚蛋好么 1.首先下去VSC官网下载.deb的安装包,安装好vsc,打开后安装PYTHON,reload下.别啊忘记修改路径,setting下搜python ...

  8. bugku 想蹭网先解开密码

    首先下载文件 然后 创建密码字典:使用命令 crunch 11 11 -t 1391040%%%% -o password.txt 爆破:使用命令 aircrack-ng -a2 所下载文件的地址 - ...

  9. 使用Echarts实现折线图的一点总结

    使用Echarts的一点总结 1.安装,我使用得vue cnpm install echarts --save 2.在入口文件main.js中注册,并使用 // 引入折线图 echarts impor ...

  10. Codeforces Round #600 (Div. 2) D。 Harmonious Graph

    #include<iostream> using namespace std ; ; int p[N]; int cnt; int find(int x) { if(p[x]!=x) p[ ...