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 ...
随机推荐
- sqlserver中的时间比较
例子: select count(*) from table where DATEDIFF ([second], '2004-09-18 00:00:18', '2004-09-18 00:00:19 ...
- HDFS源码分析之编辑日志编辑相关双缓冲区EditsDoubleBuffer
EditsDoubleBuffer是为edits准备的双缓冲区.新的编辑被写入第一个缓冲区,同时第二个缓冲区可以被flush.为edits准备的双缓冲区.新的编辑被写入第一个缓冲区,同时第二个缓冲区可 ...
- linux中likely()和unlikely()
likely()与unlikely()在2.6内核中,随处可见,那为什么要用它们?它们之间有什么区别呢?首先明确: if (likely(value))等价于if (value) if (unlike ...
- 1-2:CSS3课程入门之结构选择
E:nth-child(n) 表示E父元素中的第n个字节点 p:nth-child(odd){background:red}/*匹配奇数行*/ p:nth-child(even){background ...
- 【Selenium + Python】路径报错之OSError: [Errno 22] Invalid argument: './t/report/2018-03-23_11:03:12_report.html'
现象: 此问题真的是太痛苦了,查了好多资料是说路径的问题,结果还是报错,后来一点点的排查才发现原来是!!!!!! 废话不多说上原来代码: if __name__ == '__main__': star ...
- linux环境tomcat配置及hadoop 2.6伪分布模式安装配置
一.ubuntu 15.04.openjdk1.7.tomcat7环境配置 1. 配置openjdk1.7,输入命令: -jdk 2. 查看java是否安装成功,输入命令: envjava -vers ...
- 点击textbox弹出对话框,返回弹出对话框的值
主要是在父页面使用 function PopupWindow() { window.open(url, "", "status=no,resizab ...
- Centos7重新安装yum
Centos7重新安装yum rpm -qa|grep yum 然后用下面的命令删除出现的xxx包: rpm -e --nodeps xxx 下载 python-urlgrabber-3.10-8.e ...
- 注册HttpHandler
How to: Register HTTP Handlers After you have created a custom HTTP handler class, you must register ...
- vue+vuex构建单页应用
基本 构建工具: webpack 语言: ES6 分号:行首分号规则(行尾不加分好, [ , ( , / , + , - 开头时在行首加分号) 配套设施: webpack 全家桶, vue 全家桶 项 ...