1135. Is It A Red-Black Tree (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:

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

Sample Output:

Yes
No
No 题意:判断是否是红黑树。
红黑树定义:1:每个节点要么红要么黑2:根节点一定是黑的3:红色节点的子节点都是黑色4:任意一个节点的左右子树上的黑色节点数相同,其实就是根节点到叶子节点的所有路径上的黑色节点个数相同即可。
思路:已给出前序遍历,按顺序插入二叉树,构建搜索树。之后判断是否满足红黑树的定义。即对2,3,4三点定义进行判断。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 30000+5
typedef long long ll;
struct Node {
int key;
Node* left, *right;
};
Node *NIL,*root;
int t,n;
void insert(Node* &root,int key) {
if (root == NIL) {
root = new Node;
root->key = key;
root->left = root->right = NIL;
return;
}
if (abs(key) > abs(root->key))
insert(root->right, key);
else
insert(root->left, key);
}
bool is_correct = ;
bool is_first = ; int tot_num = ;
void judge(Node* root,int num) {
if (root == NIL) {
if (!is_first) {
tot_num = num;
is_first = ;
}
else if (num != tot_num)is_correct = false;
return;
}
if (root->key < ) {//当前节点是红色
if ((root->left != NIL&&root->left->key < ) || (root->right != NIL&&root->right->key < )) {
is_correct = false; return;
}
judge(root->left,num);
judge(root->right,num);
}
else {//当前黑色节点,num数量加1
judge(root->left, num+);
judge(root->right, num+);
}
} int main() {
scanf("%d", &t);
while (t--) {
root = NIL; is_first = ,tot_num = ,is_correct = true;
scanf("%d",&n);
for (int i = ; i < n;i++) {
int data; scanf("%d",&data);
if (!i&&data < )is_correct = false;
insert(root,data);
}
if (!is_correct) { printf("No\n"); continue; }
judge(root, );
if (is_correct)puts("Yes");
else puts("No");
}
}

pat 甲级 1135. Is It A Red-Black Tree (30)的更多相关文章

  1. 【PAT甲级】1099 Build A Binary Search Tree (30 分)

    题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...

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

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

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

  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

    https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 There is a kind of bal ...

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

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

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

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

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

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

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

随机推荐

  1. 进入docker容器并执行命令的的3中方法

    进入docker容器并执行命令的的3中方法 docker exec   nsenter   docker attach "container" 建议使用nsenter, exec有 ...

  2. 九、MySQL 创建数据表

    MySQL 创建数据表 创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: CREATE TABLE table_name (col ...

  3. JS如何判断是否为ie浏览器的方法(包括IE10、IE11在内)

    判断是否IE浏览器用的是window.navigator.userAgent,跟踪这个信息,发现在开发环境,识别为IE10,但访问服务器则识别为IE11,但IE11的userAgent里是没有MSIE ...

  4. 第9课 文章模块分析及建表 Thinkphp5商城第四季

    目录 文章模块的分析 表结构 文章模块的分析 表结构 CREATE TABLE `tp_cate` ( `id` smallint(6) NOT NULL AUTO_INCREMENT COMMENT ...

  5. Dire Wolf HDU - 5115(区间dp)

    Dire Wolf Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Total ...

  6. Docker从零到实践过程中的坑

    欢迎指正: Centos7 下的ulimit在Docker中的坑 http://www.dockone.io/article/522 僵尸容器:Docker 中的孤儿进程 https://yq.ali ...

  7. linux lvm扩容

    1.分区,  查看磁盘使用:fdisk -l 对磁盘分区:fdisk /dev/sdb 2.创建pv pvcreate /dev/sdb1 查看pv: pvdisplay 3.查看vg  vgdisp ...

  8. Reading comprehension HDU - 4990 (矩阵快速幂 or 快速幂+等比数列)

    ;i<=n;i++) { )ans=(ans*+)%m; %m; } 给定n,m.让你用O(log(n))以下时间算出ans. 打表,推出 ans[i] = 2^(i-1) + f[i-2] 故 ...

  9. Codeforces Round #461 (Div. 2) C. Cave Painting

    C. Cave Painting time limit per test 1 second memory limit per test 256 megabytes Problem Descriptio ...

  10. JVM垃圾回收--年轻代、年老点和持久代(转)

      关键字约定 Young generation –>新生代 Tenured / Old Generation –>老年代 Perm Area –>永久代 年轻代: 所有新生成的对象 ...