判断一棵二叉树是否完全二叉树。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<vector>
  5. #include<map>
  6. #include<queue>
  7. #include<stack>
  8. #include<algorithm>
  9. using namespace std;
  10.  
  11. int n,root;
  12. const int maxn=;
  13. struct Node
  14. {
  15. int left;
  16. int right;
  17. int dep;
  18. } s[maxn];
  19. int flag[maxn];
  20. vector<int>g[maxn];
  21. int Max_dep=;
  22.  
  23. void dfs(int x,int dep)
  24. {
  25. Max_dep=max(dep,Max_dep);
  26. s[x].dep=dep;
  27. g[dep].push_back(x);
  28.  
  29. if(s[x].left!=-) dfs(s[x].left,dep+);
  30. if(s[x].right!=-) dfs(s[x].right,dep+);
  31. }
  32.  
  33. int main()
  34. {
  35. memset(flag,,sizeof flag);
  36. scanf("%d",&n);
  37. for(int i=; i<n; i++)
  38. {
  39. char L[],R[];
  40. scanf("%s%s",L,R);
  41. if(L[]=='-') s[i].left=-;
  42. else
  43. {
  44. int num=;
  45. for(int k=; L[k]; k++) num=num*+L[k]-'';
  46. s[i].left=num;
  47. flag[num]=;
  48. }
  49.  
  50. if(R[]=='-') s[i].right=-;
  51. else
  52. {
  53. int num=;
  54. for(int k=; R[k]; k++) num=num*+R[k]-'';
  55. s[i].right=num;
  56. flag[num]=;
  57. }
  58. }
  59.  
  60. for(int i=; i<n; i++)
  61. if(flag[i]==) root=i;
  62.  
  63. dfs(root,);
  64.  
  65. if(Max_dep==)
  66. {
  67. printf("YES %d\n",g[Max_dep][g[Max_dep].size()-]);
  68. }
  69. else
  70. {
  71. bool fail=;
  72. for(int i=; i<=Max_dep; i++)
  73. {
  74. if(i<Max_dep)
  75. {
  76. if(g[i].size()==(int)pow(2.0,i)) {}
  77. else fail=;
  78. }
  79. else
  80. {
  81. for(int j=; j<g[i].size(); j=j+)
  82. {
  83. if(j+<g[i].size()&&j<g[i].size())
  84. {
  85. if(s[g[i-][j/]].left==g[i][j]&&s[g[i-][j/]].right==g[i][j+]) {}
  86. else fail=;
  87. }
  88. else
  89. {
  90. if(s[g[i-][j/]].left==g[i][j]) {}
  91. else fail=;
  92. }
  93. }
  94. }
  95. }
  96.  
  97. if(fail==) printf("NO %d\n",root);
  98. else printf("YES %d\n",g[Max_dep][g[Max_dep].size()-]);
  99. }
  100. return ;
  101. }

PAT (Advanced Level) 1110. Complete Binary Tree (25)的更多相关文章

  1. 【PAT甲级】1110 Complete Binary Tree (25分)

    题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点 ...

  2. [二叉树建树&完全二叉树判断] 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 ...

  3. 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 ...

  4. 1110. Complete Binary Tree (25)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  5. 1110 Complete Binary Tree (25 分)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  6. 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 ...

  7. PAT甲题题解-1110. Complete Binary Tree (25)-(判断是否为完全二叉树)

    题意:判断一个节点为n的二叉树是否为完全二叉树.Yes输出完全二叉树的最后一个节点,No输出根节点. 建树,然后分别将该树与节点树为n的二叉树相比较,统计对应的节点个数,如果为n,则为完全二叉树,否则 ...

  8. PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

    因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include& ...

  9. PAT甲级——1110 Complete Binary Tree (完全二叉树)

    此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830   1110 Complete Binary ...

随机推荐

  1. redis数据类型:hashes

    redis hash 是一个string类型的field和value的映射表. 它的添加.删除操作都是O(1)(平均),hash特别适合用于存储对象 将一个对象存储在hash类型总会占用更少的内存,并 ...

  2. 使用SpringMVC时,配置DispatcherServlet注意的url-pattern的问题

    url-pattern配置时注意: <!--springMVC配置--><servlet> <servlet-name>springMVC</servlet- ...

  3. linux命令英文缩写的含义(方便记忆)

    命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户 rpm:redhat package manager 红帽子打包 ...

  4. Entity Framework Tools install to VS 2015

    因为在VS2013,2015里不再支持Sql compact 数据库的显示, 但是我们可以通过安装EF tools扩展来支持,参考地址:http://thedatafarm.com/data-acce ...

  5. MVC view视图获取Html.RenderAction方式带来的参数

    通过Html.RenderAction这种方式传递的参数,在view视图中获取要使用viewContext上下文来获取:Html.ViewContext.RouteData.Values[" ...

  6. HDU1007--Quoit Design(平面最近点对)

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

  7. python unitest基本

    基本 import unittest class OneTest(unittest.TestCase): def setUp(self): self.verificationErrors = [] s ...

  8. angular 搜索记录保留

    #方法1: 点击后退到home后,再点击搜索, locationChangeStart 事件会多次触发. # $scope.keyword = $location.search().search # ...

  9. 在windows命令行窗口下执行:查看所有的端口占用情况

    开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...

  10. listview条目用状态选择器没反应

    button和imagebutton天生具有“可点击(click)”.“可按下(press)”的特性,radiobutton具有“可勾选(check)”的特性,但是listview的条目只有“可按下( ...