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 ...
随机推荐
- mariaDB安装完成后设置root密码等初始化操作
修改root密码1.以root身份在终端登陆(必须)2.输入 mysqladmin -u root -p password ex后面的 ex 是要设置的密码3.回车后出现 Enter password ...
- PHP CURL 代理发送数据
$session = curl_init($request); curl_setopt ($session, CURLOPT_PROXY, $proxy); curl_setopt ($session ...
- csv格式导出文件
先上传连个图片看看效果,这是界面效果dwz框架(springmvc开发) 点击导出csv效果图 js部分的代码(带条件查询的csv导出): function exportReportCsv(){ ex ...
- android打包大小笔录
版本 4.6.1 1-1 游戏完整包的大小 1-2 1级代码压缩 1-3 2级代码压缩 1-4 3级代码压缩 2-1 删除核心战斗的部分资源 2-2 2级代码压缩 3-0 删除外部UI的资源 剩下 ...
- iOS SDWEBImage和collectionView的组合,以及collectionView的随意间距设置
转载自:http://www.cnblogs.com/tmf-4838/p/5361271.html #import "ViewController.h" #import < ...
- HDU - 1045 Fire Net(二分匹配)
Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...
- Redis简介二
一.直接安装 1.Windows版本的Redis下载地址:https://github.com/dmajkic/redis/downloads ,选择一个你想要下载的版本下载即可~ ...
- 关于PS的一些总结
1.设计给的图,单独用里边的个别图层 打开图 — 新建一个图层(ctrl+n) — (点开上面的窗口排列-垂直排列,左下边下边自动选择改成图层)—选择移动工具,选中要移动的图层,拉到新建文件夹中. ...
- web开发——水到渠成
1.tomcat中设置编码格式: 打开server.xml,添加下面红色框框里面的内容.
- ExecutorService的submit(Runnable x)和execute(Runnable x) 两个方法的本质区别
Runnable任务没有返回值,而Callable任务有返回值.并且Callable的call()方法只能通过ExecutorService的submit(Callable <T> tas ...