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 ...
随机推荐
- Java 9 模块解耦的设计策略
1. 概述 Java 平台模块系统 (Java Platform Module System,JPMS)提供了更强的封装.更可靠且更好的关注点分离. 但所有的这些方便的功能都需要付出代价.由于模块化的 ...
- mysql忘记root密码且忘了安装目录如何修改root密码
问题背景 很久之前在本机上安装mysql,也没用过(主要是用Oracle),导致root密码忘记.更严重的是,连自己的安装目录都忘记了. 遇到的问题 1.在任务管理器可以找到mysql的服务已经起来, ...
- 启动Eclipse时,启不起来JVM terminated. Exit code=-1
启动Eclipse时,启不起来JVM terminated. Exit code=-1 出现错误了,不知道什么原因原本好好的Eclipse,今天早上出问题了,启动不起来还抛出JVM terminate ...
- Npm 被公司墙解决方法
npm被公司墙了,不能用npm安装任何包应用了. npm ERR! Darwin npm ERR! argv "/usr/local/Cellar/node/6.4.0/bin/node&q ...
- hibernate出现 org.hibernate.PropertyNotFoundException: field [departmen] not found on cn.itcast.hibernate.domain.Employee1错误
hibernate出现 org.hibernate.PropertyNotFoundException: field [departmen] not found on cn.itcast.hibern ...
- 把对象写入Postgresql中
工作中,遇到把大对象写入Postgresql数据库中 package com.geni_sage.gdme.cws.dic; import java.io.BufferedInputStream; i ...
- 删除rabbitmq的队列和队列中的数据
访问http://{rabbitmq安装IP}:15672,帐号guest,密码guest(也可以使用自己创建的帐号). 登录后访问http://{rabbitmq安装IP}:15672/#/qu ...
- 21-nginx单机1W并发优化
一:优化思路 (1)建立socket连接 (2)打开文件,并沿socket返回.二:优化 (1) 修改nginx.conf 进程数量 默认是1024 改成20140 worker_rlimit_no ...
- CSS -- 未解之疑
@.css那些事儿 -- 第9章 反馈表单 自己编写了CSS,可是红框中的横线比下面的要粗.对比作者的代码,发现可能与上面标题h3的height和line-height有关,但是不知道具体是为什么? ...
- iOS 可选择的购物车
最近看了淘宝的购物车,于是做了一个可选择的购物车模板. 如果有好的建议请提出,带我日后更新.