PAT 甲级 1135 Is It A Red-Black Tree
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:
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
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 500;
int T, n; int preorder[maxn]; struct Node {
int child[2];
int blackCnt;
int value;
int color;
}s[maxn]; int root, sz, ans; int AddNode(int valAndcol) {
sz ++;
s[sz].value = abs(valAndcol);
s[sz].color = valAndcol >= 0;
return sz;
} void Build(int L, int R, int father, bool direction) {
int l1 = -1, r1 = -1;
int l2 = -1, r2 = -1; for(int i = L + 1; i <= R; i ++) {
if(abs(preorder[i]) < abs(preorder[L])) {
l1 = L + 1, r1 = i;
} else if(abs(preorder[i]) == abs(preorder[L])) {
ans = 0;
return;
} else {
if(l2 == -1) l2 = i, r2 = R;
}
} if(l1 != -1) {
for(int i = l1; i <= r1; i ++) {
if(abs(preorder[i]) >= abs(preorder[L])) {
ans = 0;
return;
}
}
} if(l2 != -1) {
for(int i = l2; i <= r2; i ++) {
if(abs(preorder[i]) <= abs(preorder[L])) {
ans = 0;
return;
}
}
} // left: [l1, r1], right: [l2, r2]
int currentNode = AddNode(preorder[L]);
father != -1 ? s[father].child[direction] = currentNode : root = currentNode;
if(l1 != -1) Build(l1, r1, currentNode, 0);
if(ans == 0) return;
if(l2 != -1) Build(l2, r2, currentNode, 1);
} void Initialize() {
ans = 1;
root = -1;
sz = 0;
for(int i = 0; i < maxn; i ++) {
s[i].child[0] = s[i].child[1] = s[i].color = -1;
s[i].blackCnt = s[i].value = 0;
}
} void dfs(int x) {
for(int i = 0; i < 2; i ++) {
if(s[x].child[i] != -1) {
dfs(s[x].child[i]);
if(ans == 0) return;
}
} if(s[x].child[0] != -1 &&
s[x].child[1] != -1 &&
s[s[x].child[0]].blackCnt != s[s[x].child[1]].blackCnt) {
ans = 0;
return;
} if(s[x].child[0] != -1) s[x].blackCnt = s[s[x].child[0]].blackCnt;
if(s[x].child[1] != -1) s[x].blackCnt = s[s[x].child[1]].blackCnt;
s[x].blackCnt += s[x].color;
} int main() {
scanf("%d", &T);
while(T --) {
scanf("%d", &n);
for(int i = 1; i <= n; i ++) {
scanf("%d", &preorder[i]);
} Initialize();
Build(1, n, -1, -1); /*
// Debug Information:
for(int i = 1; i <= sz; i ++) {
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);
}
*/ // (1) Every node is either red or black. // (2) The root is black.
if(!s[root].color) ans = 0; // (3) Every leaf (NULL) is black. // (4) If a node is red, then both its children are black.
for(int i = 1; i <= sz; i ++) {
if(!s[i].color) {
if(s[i].child[0] != -1 && !s[s[i].child[0]].color) ans = 0;
if(s[i].child[1] != -1 && !s[s[i].child[1]].color) ans = 0;
}
} for(int i = 1; i <= n; i ++) {
for(int j = 0; j < 2; j ++) {
if(s[i].child[j] == -1) {
s[i].child[j] = AddNode(0);
}
}
} // (5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
dfs(root); printf("%s\n", ans ? "Yes" : "No");
}
return 0;
}
PAT 甲级 1135 Is It A Red-Black Tree的更多相关文章
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- 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 ...
- 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 ...
- PAT甲级——1135 Is It A Red-Black Tree (30 分)
我先在CSDN上面发表了同样的文章,见https://blog.csdn.net/weixin_44385565/article/details/88863693 排版比博客园要好一些.. 1135 ...
- PAT甲级1135 Is It A Red-Black Tree?【dfs】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 题意: 给定一棵二叉搜索树的先序遍历结 ...
- 【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 ...
- PAT甲级1123 Is It a Complete AVL Tree【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...
- PAT 甲级 1043 Is It a Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...
- 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 ...
随机推荐
- SpringMVC 常用applicationContext.xml、web.xml、servlet-mvc.xml简单配置
在进行学习配置文件之前,为了加深对框架的认识,简单的做了SSM框架的简单实验.然后画出listAll查询方法的整个过程的思维导图. 整个过程中的web.xml.SpringMVC.xml.applic ...
- linux(centos 7)下安装elasticsearch - head插件(端口占用,防火墙关闭)
本文章来自网络仅供个人学习记录之用 一:安装Git(如果未安装) 1, yum install git 2, git --version #查看版本 二:安装node(如果未安装) node安装 三: ...
- npm和node的版本过低时的解决办法
npm版本过低时的解决办法npm全名Node Package Manager 1.配置源的三种方法:1).npmrc文件的作用,就是配置npm源:使用淘宝源的方法就是在文件.npmrc中加入下面的语句 ...
- rac添加新节点的步骤与方法(官方步骤与自我测试)
Extending the Oracle Grid Infrastructure Home to the New NodeNow that the new node has been configur ...
- 'style-loader', 'css-loader'使用
'style-loader', 'css-loader'使用 1.安装 npm install style-loader css-loader --save-dev 2.配置 webpack.conf ...
- 【Codeforces 526D】Om Nom and Necklace
Codeforces 526 D 题意:给一个字符串,求每个前缀是否能表示成\(A+B+A+B+\dots+A\)(\(k\)个\(A+B\))的形式. 思路1:求出所有前缀的哈希值,以便求每个子串的 ...
- Skyline中的GDAL
安装Skyline的TerraExplorer Pro软件后,我们很容易在其安装目录中找到这样一些文件: gdal.dll.gdal_csharp.dll.ogr_csharp.dll.osr_csh ...
- IIS 8的第一次请求不变慢如何配置
首先需要在Window中添加Application Initialization 在IIS中配置Application Pool 在IIS配置Web Site 配置完成,如果版本在7.5,可以下载:A ...
- 重装系统之U盘设为第一启动项
做好启动盘之后(教程:重装系统之制作U盘启动盘),接下来该设置U盘为第一启动项. 以我的电脑(华硕X450JN)为例,开机不停地按f2,进入系统引导界面. 其它牌子的电脑可以在开机时候试试esc,f1 ...
- C#的抽象类别
抽象类,有3个特点: 第1个,不能被实例化. 第2个,类中的抽象方法在继承的子类中必须重写. 第3个,类一旦有抽象方法出现,那这个类必须定义为抽象类. 现实开发中,发现有共同的代码,可以把这些共同的代 ...