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. 使用IP地址方法登录MySQL数据库Can't connect to MySQL server的原因。mysql -h 192.168.1.104 -P3306 -uroot -p 失败

    mysql -h 192.168.1.104 -P3306 -uroot -p 然后输入你安装时设置的MySQL密码 发现Can't connect to MySQL server 你的IP 解决方法 ...

  2. go包的理解

    结论: import时指定的字符串,是相对于$GOPATH的目录路径,告诉了go,要去加载这个目录下所有的包文件(不包括子目录中的文件) 一个目录中所有的源文件(不包括子目录)代表了单独的一个包,这些 ...

  3. JZOJ 5849 d

    Description Input Output Data Constraint 做法:考虑贪心使得mina*minb最大,先以a为关键字排序,然后枚举删除最小的0~m个ai,对应删除最小的bi,然后 ...

  4. hadoop核心组件概述及hadoop集群的搭建

    什么是hadoop? Hadoop 是 Apache 旗下的一个用 java 语言实现开源软件框架,是一个开发和运行处理大规模数据的软件平台.允许使用简单的编程模型在大量计算机集群上对大型数据集进行分 ...

  5. 基于neo4j图数据库,实现人口关系大图的基本思路及实现方案。

    近期由于工作需要,需要做一个人口关系大图的存储及检索方案,我们主要的数据对象有:人口(年龄,身份证号码,性别..) :学校信息(学校地址,学校名称,学校级别,学校下边的年级班级..):就职信息(公司名 ...

  6. 牛客练习赛42 A 字符串

    题目描述 给定两个等长的由小写字母构成的串 A,BA,B,其中 |A|=|B|=n|A|=|B|=n. 现在你需要求出一个子区间 [l,r][l,r] 使得 LCP(A[l,r],B[l,r])×LC ...

  7. Diycode开源项目 LoginActivity分析

    1.首先看一下效果 1.1.预览一下真实页面 1.2.分析一下: 要求输入Email或者用户名,点击编辑框,弹出键盘,默认先进入输入Email或用户名编辑框. 点击密码后,密码字样网上浮动一段距离,E ...

  8. 大数据面试(HR电话了解)

    1什么是HA集群? 所谓HA,即高可用(7*24小时不中断服务) HA集群是hadoop高可用集群,即有两个namenode,一个active,一个stanby,active的name挂掉之后,sta ...

  9. Windows Server 2012 R2:细节信息汇总

    Windows Server 2012 R2:细节信息汇总 2013年08月09日00:10 it168网站原创 作者:核子可乐编译 编辑:王晓东 我要评论(0) 标签: 操作系统 , Windows ...

  10. 『编写高质量代码Web前端开发修炼手册』读书笔记--高质量的CSS

    1.怪异模式和DTD 标准模式:浏览器根据规范表现页面 怪异模式:模拟老浏览器行为防止老站点无法工作(为了兼容老式浏览器的代码),如果漏写DTD(Document Type Definition文档定 ...