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 ...
随机推荐
- 怎样正确设置remote_addr和x_forwarded_for
怎样正确设置remote_addr和x_forwarded_for 2013-04-20 做网站时经常会用到remote_addr和x_forwarded_for这两个头信息来获取客户端的IP,然而当 ...
- 使用Erlang和Yaws开发REST式的服务
看过那张很出名的“Apache vs. Yaws”图么?是不是在考虑你也应该使用Yaws了?这些图给人的第一印象是,Yaws在可伸缩性上具有难以置信的巨大优势,它可以扩展到80000个并行的连接,而 ...
- 2015 Multi-University Training Contest 7
1001 Game On the Tree 1002 Tree Maker 1003 Hotaru's problem Manacher处理好p数组. 暴力举一下公共串即可. # include &l ...
- sha加密算法
密钥生成 公钥(e,n) 私钥(d,n) 找两个互质的大素数p和q, 计算n=p*p, f(n)=(p-1)*(q-1) 选择随机整数e(e和f(n)互质) de=f(n)mod 1 利用公钥加密 ...
- eclipse中maven工程的创建javaweb项目
第一步.new一个新的工程 ,选中maven project,点击next 第2步.不做选择,点击next 第3步.选择maven-archetype-webapp,点击next 第四步.填写项目名称 ...
- yaf for ubuntu安装
一.安装yaf需要的扩展 apt-get install perl-modules apt-get install libpcrecpp0 apt-get install libpcre3 libpc ...
- iOStextView的代理方法展示
UITextView的代理方法 textViewShouldBeginEditing: and textViewDidBeginEditing: - (BOOL)textViewShouldBegin ...
- 部署php项目
1.不写端口号,即127.0.0.1,其隐含一个默认端口号80 2.查看当前占用的端口号 3.nginx修改工作路径,nginx修改端口号 4.查看端口号是否可访问telnet命令 在dos界面输入命 ...
- Apriori算法-位运算-C语言
原文地址:http://blog.csdn.net/liema2000/article/details/6118423 //////////////////////////////////////// ...
- php mysql数据库 分页与搜索
<?php/** * Created by coder meng. * User: coder meng * Date: 2016/8/29 10:27 */header("Conte ...