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 ...
随机推荐
- tf树
tf变换(1) TF库的目的是实现系统中任一个点在所有坐标系之间的坐标变换,也就是说,只要给定一个坐标系下的一个点的坐标,就能获得这个点在其他坐标系的坐标. 使用tf功能包,a. 监听tf变换: ...
- Angular 一些问题(跨域,后台接收不到参数)
1,跨域:跟前端没多大关系的,后台没设置头而已.这时候如果你们后端太菜你可以叫他加上每种语言 都不同,但是里面的呢荣是一样的.具体跨域可以跳转这里http://www.cnblogs.com/dojo ...
- 也谈SQL Server 2008 处理隐式数据类型转换在运行计划中的增强 (续)
在上一篇文章也谈SQL Server 2008 处理隐式数据类型转换在运行计划中的增强中,我提到了隐式数据类型转换添加对于数据分布非常不平均的表.评估的数据行数与实际值有非常大出入的问题,进一步測试之 ...
- JavaScript中setInterval用法
setInterval动作的作用是在播放动画的时,每隔一定时间就调用函数,方法或对象.可以使用本动作更新来自数据库的变量或更新时间显示. setInterval动作的语法格式如下:setInterva ...
- PowerBuilder -- 日期
#PB自带日期相关函数 Date(...), DateTime(...), RelativeDate(...), Year(...), Month(...), Day(...), DaysAfter( ...
- 查询SQL2008字段和注释
SELECT 表名 then d.name else '' end, 表说明 then isnull(f.value,'') else '' end, 字段序号 = a.colorder, 字段名 = ...
- JVM内存布局及GC知识回顾
注:本文篇幅较长,且需要有一定的java基础,建议各位看官,备好瓜子.饮料.小板凳,摆个让自己舒服的姿势,慢慢细看^_^, 文中所有素材,均来自互联网,本人只是详细梳理了一遍,形成此文. 一.JVM运 ...
- iOS 流布局 UICollectionView使用(使用FlowLayout进行更灵活布局)
在UICollectionView的布局中,如果每个item的大小都一样那么是十分简单的事情,但是,如果我们想要的每个item大小不一样呢,这个时候,就要对UICollectionViewFlowLa ...
- wxPython的Refresh与事件双重响应
#!/usr/bin/env python import wx class DoubleEventFrame(wx.Frame): def __init__(self, parent, id): wx ...
- 【题解】DZY Loves Chinese
[题解]DZY Loves Chinese II 不吐槽这题面了... 考虑如何维护图的连通性,如果把图的变成一颗的\(dfs\)生成树,那么如果把一个节点的父边和他接下来所有的返祖边删除,那么我们就 ...