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 ...
随机推荐
- Orcad CIS怎么批量修改字体大小
选中DSN,右键,design properties, schematic design,选择design properties.
- mysql 让一个存储过程定时作业的代码(转)
1.在mysql 中建立一个数据库 test1 语句:create database test1 2.创建表examinfo create table examinfo( id int auto_in ...
- Android 开源项目精选
0x00 leakcanary [内存泄漏检测] Leakcanary : A memory leak detection library for Android and Java. 良心企业Squ ...
- ExtJs5.1多选下拉框CheckComb
ExtJs这么多个版本号了.可就是不提供多选下拉框,老外不用这个玩意吗? 5都出来这么久了,新写的项目就用5吧,把曾经Extjs4.2的时搜到前人的CheckComb改巴改巴.能用了就赶紧贴上来,没有 ...
- 黑帽SEO手法
黑帽SEO手法 0x00:概念 SEO全称搜索引擎优化,通过站内优化和站内优化方式来提升搜索引擎排名,有白帽SEO和黑帽SEO. 因为正规的SEO优化需要很长时间,黑帽SEO手法让站内快速提升排名的有 ...
- Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml, reason: Connect
Failed to fetch URL https://dl-ssl.google.com/android/repository/addons_list-2.xml, reason: Connect ...
- ReactNative Navigator
https://facebook.github.io/react-native/docs/navigator.html Navigator实现了页面之间的跳转. Demo描述:打开即进入“课程”页面, ...
- 【Python + Selenium】初次用IE浏览器之报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones.
初次用IE浏览器运行自动化程序时,报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launchi ...
- <转载> pycharm快捷键及一些常用设置
1.编辑(Editing ) Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意类Ctrl + Shift + Enter 语句完成Ctrl ...
- 九度OJ 1061:成绩排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:17158 解决:4798 题目描述: 有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相 ...