https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640

There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties:

  • (1) Every node is either red or black.
  • (2) The root is black.
  • (3) Every leaf (NULL) is black.
  • (4) If a node is red, then both its children are black.
  • (5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.

For example, the tree in Figure 1 is a red-black tree, while the ones in Figure 2 and 3 are not.

Figure 1 Figure 2 Figure 3

For each given binary search tree, you are supposed to tell if it is a legal red-black tree.

Input Specification:

Each input file contains several test cases. The first line gives a positive integer K (≤30) which is the total number of cases. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the preorder traversal sequence of the tree. While all the keys in a tree are positive integers, we use negative signs to represent red nodes. All the numbers in a line are separated by a space. The sample input cases correspond to the trees shown in Figure 1, 2 and 3.

Output Specification:

For each test case, print in a line "Yes" if the given tree is a red-black tree, or "No" if not.

Sample Input:

  1. 3
  2. 9
  3. 7 -2 1 5 -4 -11 8 14 -15
  4. 9
  5. 11 -2 1 -7 5 -4 8 14 -15
  6. 8
  7. 10 -7 5 -6 8 15 -11 17

Sample Output:

  1. Yes
  2. No
  3. No

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn = 500;
  5. int T, n;
  6.  
  7. int preorder[maxn];
  8.  
  9. struct Node {
  10. int child[2];
  11. int blackCnt;
  12. int value;
  13. int color;
  14. }s[maxn];
  15.  
  16. int root, sz, ans;
  17.  
  18. int AddNode(int valAndcol) {
  19. sz ++;
  20. s[sz].value = abs(valAndcol);
  21. s[sz].color = valAndcol >= 0;
  22. return sz;
  23. }
  24.  
  25. void Build(int L, int R, int father, bool direction) {
  26. int l1 = -1, r1 = -1;
  27. int l2 = -1, r2 = -1;
  28.  
  29. for(int i = L + 1; i <= R; i ++) {
  30. if(abs(preorder[i]) < abs(preorder[L])) {
  31. l1 = L + 1, r1 = i;
  32. } else if(abs(preorder[i]) == abs(preorder[L])) {
  33. ans = 0;
  34. return;
  35. } else {
  36. if(l2 == -1) l2 = i, r2 = R;
  37. }
  38. }
  39.  
  40. if(l1 != -1) {
  41. for(int i = l1; i <= r1; i ++) {
  42. if(abs(preorder[i]) >= abs(preorder[L])) {
  43. ans = 0;
  44. return;
  45. }
  46. }
  47. }
  48.  
  49. if(l2 != -1) {
  50. for(int i = l2; i <= r2; i ++) {
  51. if(abs(preorder[i]) <= abs(preorder[L])) {
  52. ans = 0;
  53. return;
  54. }
  55. }
  56. }
  57.  
  58. // left: [l1, r1], right: [l2, r2]
  59. int currentNode = AddNode(preorder[L]);
  60. father != -1 ? s[father].child[direction] = currentNode : root = currentNode;
  61. if(l1 != -1) Build(l1, r1, currentNode, 0);
  62. if(ans == 0) return;
  63. if(l2 != -1) Build(l2, r2, currentNode, 1);
  64. }
  65.  
  66. void Initialize() {
  67. ans = 1;
  68. root = -1;
  69. sz = 0;
  70. for(int i = 0; i < maxn; i ++) {
  71. s[i].child[0] = s[i].child[1] = s[i].color = -1;
  72. s[i].blackCnt = s[i].value = 0;
  73. }
  74. }
  75.  
  76. void dfs(int x) {
  77. for(int i = 0; i < 2; i ++) {
  78. if(s[x].child[i] != -1) {
  79. dfs(s[x].child[i]);
  80. if(ans == 0) return;
  81. }
  82. }
  83.  
  84. if(s[x].child[0] != -1 &&
  85. s[x].child[1] != -1 &&
  86. s[s[x].child[0]].blackCnt != s[s[x].child[1]].blackCnt) {
  87. ans = 0;
  88. return;
  89. }
  90.  
  91. if(s[x].child[0] != -1) s[x].blackCnt = s[s[x].child[0]].blackCnt;
  92. if(s[x].child[1] != -1) s[x].blackCnt = s[s[x].child[1]].blackCnt;
  93. s[x].blackCnt += s[x].color;
  94. }
  95.  
  96. int main() {
  97. scanf("%d", &T);
  98. while(T --) {
  99. scanf("%d", &n);
  100. for(int i = 1; i <= n; i ++) {
  101. scanf("%d", &preorder[i]);
  102. }
  103.  
  104. Initialize();
  105. Build(1, n, -1, -1);
  106.  
  107. /*
  108. // Debug Information:
  109. for(int i = 1; i <= sz; i ++) {
  110. printf("Id: %d, L: %d, R: %d, val: %d, col: %d\n", i, s[i].child[0], s[i].child[1], s[i].value, s[i].color);
  111. }
  112. */
  113.  
  114. // (1) Every node is either red or black.
  115.  
  116. // (2) The root is black.
  117. if(!s[root].color) ans = 0;
  118.  
  119. // (3) Every leaf (NULL) is black.
  120.  
  121. // (4) If a node is red, then both its children are black.
  122. for(int i = 1; i <= sz; i ++) {
  123. if(!s[i].color) {
  124. if(s[i].child[0] != -1 && !s[s[i].child[0]].color) ans = 0;
  125. if(s[i].child[1] != -1 && !s[s[i].child[1]].color) ans = 0;
  126. }
  127. }
  128.  
  129. for(int i = 1; i <= n; i ++) {
  130. for(int j = 0; j < 2; j ++) {
  131. if(s[i].child[j] == -1) {
  132. s[i].child[j] = AddNode(0);
  133. }
  134. }
  135. }
  136.  
  137. // (5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
  138. dfs(root);
  139.  
  140. printf("%s\n", ans ? "Yes" : "No");
  141. }
  142. return 0;
  143. }

 

PAT 甲级 1135 Is It A Red-Black Tree的更多相关文章

  1. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  2. PAT 甲级1135. Is It A Red-Black Tree (30)

    链接:1135. Is It A Red-Black Tree (30) 红黑树的性质: (1) Every node is either red or black. (2) The root is ...

  3. pat 甲级 1135. Is It A Red-Black Tree (30)

    1135. Is It A Red-Black Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  4. PAT甲级——1135 Is It A Red-Black Tree (30 分)

    我先在CSDN上面发表了同样的文章,见https://blog.csdn.net/weixin_44385565/article/details/88863693 排版比博客园要好一些.. 1135 ...

  5. PAT甲级1135 Is It A Red-Black Tree?【dfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 题意: 给定一棵二叉搜索树的先序遍历结 ...

  6. 【PAT 甲级】1151 LCA in a Binary Tree (30 分)

    题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has bo ...

  7. PAT甲级1123 Is It a Complete AVL Tree【AVL树】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...

  8. PAT 甲级 1043 Is It a Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...

  9. PAT甲级——1123 Is It a Complete AVL Tree (完全AVL树的判断)

    嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...

随机推荐

  1. UVA10129-Play on Words(欧拉路径)

    Problem UVA10129-Play on Words Accept: 2534  Submit: 19477 Time Limit: 3000 mSec Problem Description ...

  2. Python调用WIN10语音交互+识别+控制+自定义对话

    1 安装库文件 2修改两个地方 最简单的 # 将输入文字转化为语音信号输出 import speech while True: speech.say("请输入:") str = i ...

  3. 数据库连性池性能测试(hikariCP,druid,tomcat-jdbc,dbcp,c3p0)

    文章转自  https://www.tuicool.com/articles/qayayiM 摘要: 本文主要是对这hikariCP,druid,tomcat-jdbc,dbcp,c3p0几种连接池的 ...

  4. springboot跨域配置

    前言: 当它请求的一个资源是从一个与它本身提供的第一个资源的不同的域名时,一个资源会发起一个跨域HTTP请求(Cross-site HTTP request).比如说,域名A ( http://dom ...

  5. Qt发起Http/Https请求

    1. BurpSuite抓包 1.1 设置代理 burpsuite代理设置 浏览器代理设置(chrome),其他浏览器同理. 地址栏输入 chrome://settings/, 打开代理设置 设置代理 ...

  6. 【Codeforces 331D3】Escaping on Beaveractor

    题意:给\(b\times b\)的网格,其中有\(n\)个不交叉的箭头. 现在有\(q\)个询问,每个询问包含一个点\((x,y)\),以及一个方向\(dir\).时间\(t\). 要求从\((x, ...

  7. Android学习之基础知识四-Activity活动7讲(活动的启动模式)

    在实际的项目开发中,我们需要根据特定的需求为每个活动指定恰当的启动模式.Activity的启动模式一共有4种:standard.singleTop.singleTask.singleInstance. ...

  8. docker命名空间、控制组及联合文件系统概念

    基本架构 命名空间 控制组 联合文件系统 docker底层依赖的核心技术主要包括操作系统的命名空间(Namespace).控制组(Control Groups).联合文件系统(Union File S ...

  9. ELF格式文件分析以及运用

    基于本文的一个实践<使用Python分析ELF文件优化Flash和Sram空间的案例>. 1.背景 ELF是Executable and Linkable Format缩写,其官方规范在& ...

  10. AIS系统(转)

    船舶自动识别系统(Automatic Identification System, 简称AIS系统)由岸基(基站)设施和船载设备共同组成,是一种新型的集网络技术.现代通讯技术.计算机技术.电子信息显示 ...