1135 Is It A Red-Black Tree(30 分)
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 <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std; struct tree {
int data;
tree *left,*right;
};
int pre[],in[];
int k,n,flag;
tree *build(int pre_l,int pre_r,int in_l,int in_r) {
tree *t = new tree();
t -> left = t -> right = NULL;
for(int i = in_l;i <= in_r;i ++) {
if(in[i] == abs(pre[pre_l])) {
if(i != in_l)t -> left = build(pre_l + ,pre_l + i - in_l,in_l,i - );
if(i != in_r)t -> right = build(pre_l + i - in_l + ,pre_r,i + ,in_r);
break;
}
}
t -> data = pre[pre_l];
return t;
}
int check(tree *t) {
if(t == NULL)return ;
if(t -> data < && (t -> left && t -> left -> data < || t -> right && t -> right -> data < )) {
flag = ;
return ;
}
int d = check(t -> left),e = check(t -> right);
if(d != e)flag = ;
return d + (t -> data > );///如果颜色为黑色,返回值加1
}
void drop(tree *t) {
if(t == NULL)return;
drop(t -> left);
drop(t -> right);
delete t;
}
int main() {
scanf("%d",&k);
while(k --) {
scanf("%d",&n);
for(int i = ;i < n;i ++) {
scanf("%d",&pre[i]);
in[i] = abs(pre[i]);///取绝对值
}
flag = ;
sort(in,in + n);
tree *head = build(,n - ,,n - );///建树
if(head -> data < )flag = ;///根结点不是黑色
else check(head);///检查是否满足
drop(head);///释放空间
puts(flag ? "Yes" : "No");
}
}
1135 Is It A Red-Black Tree(30 分)的更多相关文章
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)
7-4 Cartesian Tree (30分) A Cartesian tree is a binary tree constructed from a sequence of distinct ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 04-树6 Complete Binary Search Tree (30 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 【PAT甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 【PAT甲级】1099 Build A Binary Search Tree (30 分)
题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...
- 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 ...
随机推荐
- Angular+Angular-Ui实现分页(代码更加简单,更加容易懂哦)
前面写了个分页,但是个人认为只能玩玩,实际业务上的话代码太繁杂(笔者想走代码.性能精简化之路[不知道哪天才能成为高手~·YY一下无伤大雅]),逻辑上有点混乱.那么今天我们来看看另外一种只实现分页没有查 ...
- Mac 下 Git 的基础命令行操作
Mac 下 Git 的基础命令行操作 sudo apt-get install git-core //安装Git 用户配置 git config --global user.name "Yo ...
- 【BZOJ2476】战场的数目 矩阵乘法
[BZOJ2476]战场的数目 Description Input 输入文件最多包含25组测试数据,每个数据仅包含一行,有一个整数p(1<=p<=109),表示战场的图形周长.p=0表示输 ...
- centos7 安装php
http://blog.csdn.net/zhaozuosui/article/details/48394409
- discuz论坛搬家
很多站长第一次做网站的时候,无奈选择了速度不是很稳定的空间,慢慢会发现有很多物美价廉速度相当快的空间 这个时候,站长在网站搬家的过程中就会遇到很多困难,今天老袋鼠给大家详细讲解一下discuz论坛搬家 ...
- (转)JavaScript中==和===的区别
== 用于比较 判断 两者相等 ==在比较的时候可以转自动换数据类型 ===用于严格比较 判断两者严格相等 ===严格比较,不会进行自动转换,要求进行比较的操作数必须类型 ...
- log4j 2 入门实例(1)
本文介绍log4j的基本概念和将日志输出到控制台的例子. 参考文章: http://www.jianshu.com/p/464058bdbc76 http://www.hankcs.com/progr ...
- turbolink 造成 link_to异常
link_to 点击之后不刷新不加载文件 send_file 只能在浏览器中打开,而不能下载 解决方法:注释gem turbolink,application.js 中移除turbolink
- JVM性能分析工具详解--MAT等
获得堆转储文件 巧妇难为无米之炊,我们首先需要获得一个堆转储文件.为了方便,本文采用的是 Sun JDK 6.通常来说,只要你设置了如下所示的 JVM 参数: -XX:+HeapDumpOnOutOf ...
- (C)结构数组
结构数组 对于大小相同但是类型不同的数组,定义结构体数组对其很有帮组.例如: char *keyword[NKEYS]; int keycount[NKEYS]; 这两个数组大小相同,因此 可以用另一 ...