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 ...
随机推荐
- redis数据类型:hashes
redis hash 是一个string类型的field和value的映射表. 它的添加.删除操作都是O(1)(平均),hash特别适合用于存储对象 将一个对象存储在hash类型总会占用更少的内存,并 ...
- 使用SpringMVC时,配置DispatcherServlet注意的url-pattern的问题
url-pattern配置时注意: <!--springMVC配置--><servlet> <servlet-name>springMVC</servlet- ...
- linux命令英文缩写的含义(方便记忆)
命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户 rpm:redhat package manager 红帽子打包 ...
- Entity Framework Tools install to VS 2015
因为在VS2013,2015里不再支持Sql compact 数据库的显示, 但是我们可以通过安装EF tools扩展来支持,参考地址:http://thedatafarm.com/data-acce ...
- MVC view视图获取Html.RenderAction方式带来的参数
通过Html.RenderAction这种方式传递的参数,在view视图中获取要使用viewContext上下文来获取:Html.ViewContext.RouteData.Values[" ...
- HDU1007--Quoit Design(平面最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- python unitest基本
基本 import unittest class OneTest(unittest.TestCase): def setUp(self): self.verificationErrors = [] s ...
- angular 搜索记录保留
#方法1: 点击后退到home后,再点击搜索, locationChangeStart 事件会多次触发. # $scope.keyword = $location.search().search # ...
- 在windows命令行窗口下执行:查看所有的端口占用情况
开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...
- listview条目用状态选择器没反应
button和imagebutton天生具有“可点击(click)”.“可按下(press)”的特性,radiobutton具有“可勾选(check)”的特性,但是listview的条目只有“可按下( ...