【PAT甲级】1110 Complete Binary Tree (25分)
题意:
输入一个正整数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分)的更多相关文章
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
- PAT 甲级 1110 Complete Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...
- 1110 Complete Binary Tree (25 分)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- 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 ...
- [二叉树建树&完全二叉树判断] 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 ...
- 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 ...
- 1110. Complete Binary Tree (25)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)
题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...
- PAT (Advanced Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树. #include<cstdio> #include<cstring> #include<cmath> #include<vec ...
随机推荐
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- gitlab 更换服务器后访问 Integrations 出现 500 错误
异常问题解决方案:问题:gitlab 更换服务器后访问 Integrations 出现 500 错误解决方案:从原服务器上将 /etc/gitlab/gitlab-secrets.json 复制过来覆 ...
- 163.扩展User模型-一对一方式扩展
一对一外键 如果你对用户验证方法authenticate没有更多的要求,就是使用username和password就可以完成用户的登录验证工作,但是想要在原来的模型的基础上添加新的字段,那么就可以使用 ...
- [JZOJ4899] 雪之国度
题目描述 雪之国度有N座城市,依次编号为1到N,又有M条道路连接了其中的城市,每一条道路都连接了不同的2个城市,任何两座不同的城市之间可能不止一条道路.雪之女王赋予了每一座城市不同的能量,其中第i座城 ...
- mysql对表中数据根据某一字段去重
要删除重复的记录,就要先查出重复的记录,这个很容易做到 注意:这是查出所有重复记录的第一条记录,需要保留,因此需要添加查询条件,查出所有的重复记录 ) ) 然后 delete from cqssc w ...
- [HAOI2016] 找相同字符 - 后缀数组,单调栈
[HAOI2016] 找相同字符 Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. \(n,m \l ...
- C++-new 的六种重载形式
当写出这样的代码的时候, 实际上有两步操作: p = new P(); 1.分配内存, 2.然后在分配好的内存之上初始化类成员. 第二步是有构造函数完成的, 第一步就是new函数的工作. 全局的new ...
- 题解 SP27102/UVA1747 【Swap Space】
SP27102 [Swap Space] 双倍经验:UVA1747 Swap Space 用(a,b)表示每个硬盘的原容量和新文件系统下的容量.分两种情况考虑:a≤b和a>b 第一类a≤b格式化 ...
- 第三篇,ajax 和 axios、fetch的区别
1.jQuery ajax $.ajax({ type: 'POST', url: url, data: data, dataType: dataType, success: function () ...
- 通过CSS禁用页面内容选中和复制操作
-moz-user-select: none;-webkit-user-select: none;-ms-user-select: none;-khtml-user-select: none;user ...