题意:

输入一个正整数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. vscode与MinGW64调试c++报错

    这个问题在刚配好环境测试的时候往往不会被发现,因为单纯的c++编译调试是没问题的.但一旦调试使用stl库的代码就会报错,而编译又没问题且可以正常运行,但在vscode的集成终端里运行不会显示任何本该显 ...

  2. 基于Python的face_recognition库实现人脸识别

    一.face_recognition库简介 face_recognition是Python的一个开源人脸识别库,支持Python 3.3+和Python 2.7.引用官网介绍: Recognize a ...

  3. app点击底部菜单切换标题

    <!DOCTYPE HTML><html><head> <meta charset="utf-8"> <meta name=& ...

  4. ReLU(inplace=True),这里的inplace=true的意思

    ReLU(inplace=True),这里的inplace=true的意思 待办 inplace=True means that it will modify the input directly, ...

  5. Execl导入系统

    文件导入功能 前台代码: Content\JS\jquery.ajaxfileupload.js<script src="~/Content/JS/jquery.ajaxfileupl ...

  6. js学习(精华帖)

    js获取当前域名.Url.相对路径和参数以及指定参数 js获取当前url组成部分 jquery操作select(增加,删除,清空)

  7. C# 元组和值元组

    C# 7.0已经出来一段时间了,大家都知道新特性里面有个对元组的优化:ValueTuple.这里利用详尽的例子详解Tuple VS ValueTuple(元组类VS值元组),10分钟让你更了解Valu ...

  8. 神舟战神笔记本使用U盘重装系统

    今天我的神舟战神笔记本电脑系统故障了,在这里记录一下使用U盘重装系统的方法: 1.插入装机U盘. 2.按开机键,然后马上按Esc键. 3.选择Boot Manager进入. 4.选择EFI USB D ...

  9. vue mvvm原理与简单实现 -- 上篇

    Object.defineProperty介绍-- let obj = {}; Object.defineProperty(obj,'school',{ configurable : true, // ...

  10. Python_包

    包 包是一种通过使用‘.模块名’来组织python模块名称空间的方式. 1. 无论是import形式还是from...import形式,凡是在导入语句中(而不是在使用时)遇到带点的,都要第一时间提高警 ...