• 1135 Is It A Red-Black Tree


    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 1Figure 2Figure 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
    2. #include
    3. using namespace std;
    4. struct node {
    5. int val;
    6. node *left;
    7. node *right;
    8. };
    9. int k, n, x, cnt;
    10. bool flag;
    11. node *build(node *r, int val) {
    12. if (r == NULL) {
    13. r = new node();
    14. r->val = val;
    15. r->left = r->right = NULL;
    16. } else if (abs(val) < abs(r->val)) {
    17. r->left = build(r->left, val);
    18. } else {
    19. r->right = build(r->right, val);
    20. }
    21. return r;
    22. }
    23. void checkcolor(node *r) {
    24. if (flag) {
    25. return;
    26. }
    27. if (r->val < 0) {
    28. if (r->left && r->left->val < 0) {
    29. flag = 1;
    30. return;
    31. }
    32. if (r->right && r->right->val < 0) {
    33. flag = 1;
    34. return;
    35. }
    36. }
    37. if (r->left) {
    38. checkcolor(r->left);
    39. }
    40. if (r->right) {
    41. checkcolor(r->right);
    42. }
    43. }
    44. void checknum(node *r, int c) {
    45. if (flag) {
    46. return;
    47. }
    48. if (r == NULL) {
    49. c++;
    50. if (cnt == -1) {
    51. cnt = c;
    52. } else {
    53. if (cnt != c) {
    54. flag = 1;
    55. return;
    56. }
    57. }
    58. } else {
    59. if (r->val > 0) {
    60. c++;
    61. }
    62. checknum(r->left, c);
    63. checknum(r->right, c);
    64. }
    65. }
    66. int main() {
    67. cin >> k;
    68. while (k--) {
    69. cin >> n;
    70. node *root = NULL;
    71. flag = 0;
    72. cnt = -1;
    73. for (int i = 0; i < n; i++) {
    74. cin >> x;
    75. root = build(root, x);
    76. }
    77. if (root->val < 0) {
    78. cout << "No" << endl;
    79. continue;
    80. }
    81. checkcolor(root);
    82. if (flag) {
    83. cout << "No" << endl;
    84. continue;
    85. }
    86. checknum(root, 1);
    87. if (flag) {
    88. cout << "No" << endl;
    89. continue;
    90. }
    91. cout << "Yes" << endl;
    92. }
    93. return 0;
    94. }

    参考博客:1135 Is It A Red-Black Tree (30分)_一只小蒟蒻的博客-CSDN博客

    1135. Is It A Red-Black Tree (30)-PAT甲级真题_柳婼的博客-CSDN博客 

  • 相关阅读:
    SpringBoot的 8 个优点
    Java容器知识体系
    找数组中最小的两个值(不排序方法)
    管理学考试题库
    设计模式(二)-创建者模式(4)-原型模式
    Vue3+vite路由配置优化(自动化导入)
    我开的游戏经常被攻击该怎么办,云服务器陷入黑洞了要怎么处理,有没有什么好的办法彻底解决攻击
    【STM32】工程配置,存储空间分别情况,常用操作
    获取Windows远程桌面端口
    阿里巴巴微服务核心手册:Spring Boot+Spring cloud+Dubbo
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/127946958