PAT (Advanced Level) 1110. Complete Binary Tree (25)
判断一棵二叉树是否完全二叉树。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; int n,root;
const int maxn=;
struct Node
{
int left;
int right;
int dep;
} s[maxn];
int flag[maxn];
vector<int>g[maxn];
int Max_dep=; void dfs(int x,int dep)
{
Max_dep=max(dep,Max_dep);
s[x].dep=dep;
g[dep].push_back(x); if(s[x].left!=-) dfs(s[x].left,dep+);
if(s[x].right!=-) dfs(s[x].right,dep+);
} int main()
{
memset(flag,,sizeof flag);
scanf("%d",&n);
for(int i=; i<n; i++)
{
char L[],R[];
scanf("%s%s",L,R);
if(L[]=='-') s[i].left=-;
else
{
int num=;
for(int k=; L[k]; k++) num=num*+L[k]-'';
s[i].left=num;
flag[num]=;
} if(R[]=='-') s[i].right=-;
else
{
int num=;
for(int k=; R[k]; k++) num=num*+R[k]-'';
s[i].right=num;
flag[num]=;
}
} for(int i=; i<n; i++)
if(flag[i]==) root=i; dfs(root,); if(Max_dep==)
{
printf("YES %d\n",g[Max_dep][g[Max_dep].size()-]);
}
else
{
bool fail=;
for(int i=; i<=Max_dep; i++)
{
if(i<Max_dep)
{
if(g[i].size()==(int)pow(2.0,i)) {}
else fail=;
}
else
{
for(int j=; j<g[i].size(); j=j+)
{
if(j+<g[i].size()&&j<g[i].size())
{
if(s[g[i-][j/]].left==g[i][j]&&s[g[i-][j/]].right==g[i][j+]) {}
else fail=;
}
else
{
if(s[g[i-][j/]].left==g[i][j]) {}
else fail=;
}
}
}
} if(fail==) printf("NO %d\n",root);
else printf("YES %d\n",g[Max_dep][g[Max_dep].size()-]);
}
return ;
}
PAT (Advanced Level) 1110. Complete Binary Tree (25)的更多相关文章
- 【PAT甲级】1110 Complete Binary Tree (25分)
题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...
- [二叉树建树&完全二叉树判断] 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 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)
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 Advanced 1102 Invert a Binary Tree (25) [树的遍历]
题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...
- PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)
题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...
- PAT (Advanced Level) 1064. Complete Binary Search Tree (30)
因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
随机推荐
- 关于oracle数据库(3)
show user ; 查看当前用户的名称 select * from tab; 查看当前用户有哪些表 删除用户 drop user jky cascade; //cascade; 意思是级联操作 ...
- 虚拟机 centos 7 nginx安装
1下载vmware 12,并安装.百度即可 2下载centos 7,将其安装在vmware 12中.百度即可,无复杂设置. 3设置vmware 中centos7能上网: a.右键计算机->管理- ...
- JPA 系列教程17-继承-独立表-TABLE_PER_CLASS
PerTable策略 每个具体的类一个表的策略 举例 这种映射策略每个类都会映射成一个单独的表,类的所有属性,包括继承的属性都会映射成表的列. 这种映射策略的缺点是:对多态关系的支持有限,当查询涉及到 ...
- 获取集合、数组后要判断为空的必要性以及根据构造器创建对象后不是null的证实
在开发过程中,凡是获取到一个集合对象,在利用或者说遍历这个集合对象之前,都要进行是否为null以及size()>0的判断,但是如果size()>0的话,不是就已经可以说明此集合对象不为nu ...
- 点赞增加的jquery写法
$('.dianzan').click(function(){ $(this).find('span').html(parseInt($(this).find('span').html())+1) } ...
- android 获取系统硬件信息
一,首先设置权限访问: <uses-permission android:name="android.permission.READ_PHONE_STATE" /> ...
- 字符串匹配之boyer-Moore算法
Boyer-Moore算法不仅效率高,而且构思巧妙,容易理解.1977年,德克萨斯大学的Robert S. Boyer教授和J Strother Moore教授发明了这种算法. 下面,我根据Moore ...
- 50、matplotlib画图示例
1.画饼图 import matplotlib.pyplot as plt >>> labels = 'frogs','hogs','dogs','logs' >>> ...
- VNC VIEWER的使用集锦
关于颜色深度的问题, 今天用VNC Viewser ,连上去之后,发现色彩可能只有8或者16位 然后修改了 sever的depth,也不好使, 于是,就修改了 client的 colourlevel ...
- 抓包工具Fidder详解(主要来抓取Android中app的请求)
今天闲着没吊事,来写一篇关于怎么抓取Android中的app数据包?工欲行其事,必先利其器,上网google了一下,发现了一款神器:Fiddler,这个貌似是所有软件开发者必备神器呀!这款工具不仅可以 ...