A1135. Is It A Red-Black Tree
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<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
bool cmp(int a, int b){
return abs(a) < abs(b);
}
int K, N;
typedef struct NODE{
struct NODE *lchild, *rchild;
int data;
}node;
void insert(node* &root, int data){
if(root == NULL){
root = new node;
root->lchild = NULL;
root->rchild = NULL;
root->data = data;
return;
}
if(abs(data) < abs(root->data))
insert(root->lchild, data);
else insert(root->rchild, data);
}
int cnt, isEqu;
void preOrder(node* root, int dp){
if(root == NULL){
dp++;
if(cnt == -){
cnt = dp;
}else{
if(cnt != dp)
isEqu = ;
}
return;
}
if(root->data > )
dp++;
preOrder(root->lchild, dp);
preOrder(root->rchild, dp);
}
int exam(node* root){
if(root->data < ) //负数为红
return ;
queue<node*> Q;
Q.push(root);
int tag = ;
while(Q.empty() == false){
node* temp = Q.front();
if(temp->data < ){
if(temp->lchild != NULL && temp->lchild->data < || temp->rchild != NULL && temp->rchild->data < ){
tag = ;
break;
}
}
Q.pop();
cnt = -, isEqu = ;
preOrder(temp, );
if(isEqu == ){
tag = ;
break;
}
if(temp->lchild != NULL)
Q.push(temp->lchild);
if(temp->rchild != NULL)
Q.push(temp->rchild);
}
return tag;
}
int main(){
scanf("%d", &K);
for(int i = ; i < K; i++){
scanf("%d", &N);
node* root = NULL;
for(int j = ; j < N; j++){
int temp;
scanf("%d", &temp);
insert(root, temp);
}
if(root == NULL)
printf("Yes\n");
else if(exam(root) == )
printf("Yes\n");
else printf("No\n");
}
cin >> N;
return ;
}
总结:
1、题意:给出一个平衡二叉搜索树的前序序列,给出红黑树的定义,检验该平衡二叉搜索树是否是红黑树。
2、给出了平衡二叉搜索树的前序序列,就可以仅仅根据前序序列建立原树,再按部就班进行检验。检验可以分别针对红黑树的要求逐条检验,首先看根。然后按照层序的顺序,对每一个节点做如下检验:1)若它是红的,检验它的左右孩子。 2)用DFS,遍历从该节点开始到叶节点(空节点)的所有路径,统计每个路径分别的黑节点总数。
3、关于建立原树,有两种办法。一是,由于搜索树的中序是从小到大的有序序列,可以先将所有节点排序得到中序序列。再按照已知前序和中序的方法,建立二叉树。二是,由于有序二叉树的先序序列的意义:根在前子树在后,且小于根的节点在左,大于的在右。所以可以直接把先序序列当作有序二叉树的插入的顺序,按顺序插入节点,得到原树。注意已知序列是有序二叉树的先序,则可以把它当作插入顺序。但已知插入顺序,这个插入顺序却不一定是先序。
A1135. Is It A Red-Black Tree的更多相关文章
- PAT A1135 Is It A Red Black Tree
判断一棵树是否是红黑树,按题给条件建树,dfs判断即可~ #include<bits/stdc++.h> using namespace std; ; struct node { int ...
- [转载] 红黑树(Red Black Tree)- 对于 JDK TreeMap的实现
转载自http://blog.csdn.net/yangjun2/article/details/6542321 介绍另一种平衡二叉树:红黑树(Red Black Tree),红黑树由Rudolf B ...
- Red–black tree ---reference wiki
source address:http://en.wikipedia.org/wiki/Red%E2%80%93black_tree A red–black tree is a type of sel ...
- Red Black Tree 红黑树 AVL trees 2-3 trees 2-3-4 trees B-trees Red-black trees Balanced search tree 平衡搜索树
小结: 1.红黑树:典型的用途是实现关联数组 2.旋转 当我们在对红黑树进行插入和删除等操作时,对树做了修改,那么可能会违背红黑树的性质.为了保持红黑树的性质,我们可以通过对树进行旋转,即修改树中某些 ...
- CF1208H Red Blue Tree
CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有 ...
- 2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)
BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. Among ...
- 计蒜客 Red Black Tree(树形DP)
You are given a rooted tree with n nodes. The nodes are numbered 1..n. The root is node 1, and m of ...
- Red Black Tree(红黑树)
(修改于 2018-05-06 15:53:22 还差删除维护操作.层序遍历没完成.维护操作没完成不想写层序遍历怎么办...) 今天下午完成了红黑树的插入的维护操作,但删除的维护操作还没有解决,删除的 ...
- ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online
题意:一棵树上有m个红色结点,树的边有权值.q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化.q次查询相互独立. 分析:数 ...
- Red Black Tree java.util.TreeSet
https://docs.oracle.com/javase/9/docs/api/java/util/SortedMap.html public interface SortedMap<K,V ...
随机推荐
- Python Note1: Pycharm的安装与使用
前言 曾经学过一段时间python,虽然现在工作了主要使用C#和C++,但是觉得还是有必要在业余的时候学习学习python,提升下自己的知识面,毕竟技多不压身,加油吧! 安装与激活Pycharm 个人 ...
- MyBatis映射文件1(增删改、insert获取自增主键值)
增删改 Mybatis为我们提供了<insert>.<update>.<delete>标签来对应增删改操作 在接口中写增删改的抽象方法 void addEmp(Em ...
- 如何使用nodejs快速搭建本地服务器
1.首先要安装好node,js 2.以下有安装包下载的链接:这里的安装包是.msi,如果要其他的,可以到菜鸟教程上去找 32 位安装包下载地址 : https://nodejs.org/dist/v4 ...
- windows浏览器访问虚拟机开的rabbitmq服务,无法访问
根据这个博主的建议 https://blog.csdn.net/csdnliuxin123524/article/details/78207427 换了一个浏览器上火狐浏览器输入“localhost: ...
- hadoop第一个例子
Java.io.URL 1.编写java程序 package com.company; import java.io.IOException; import java.io.InputStream; ...
- Maven依赖范围及传递
.Maven因为执行一系列编译.测试和部署运行等操作,在不同的操作下使用的classpath不同,依赖范围就是用来控制依赖与三种 classpath(编译classpath.测试classpath.运 ...
- BZOJ 1443 游戏(二分图博弈)
新知识get. 一类博弈问题,基于以下条件: 1.博弈者人数为两人,双方轮流进行决策.2.博弈状态(对应点)可分为两类(状态空间可分为两个集合),对应二分图两边(X集和Y集).任意合法的决策(对应边) ...
- 接口由40秒到200ms优化记录
场景还原 一个业务逻辑较为复杂的业务,涉及到n次遍历,其中有循环查询/更新数据库,事务的管理,加上一些业务逻辑的计算.最初的接口,纯粹按照产品提供的相关业务逻辑,单纯的编码,耗时较长,近40秒的处理时 ...
- Android Studio 显示 logcat
首先调出 logcat,在整个android studio的左下角(藏的好,尼玛.) 接着设置下面三个红框 1 选择你自己的模拟器. 2 选择Warm 3 选择not filter
- python成长之路一
1,计算机基础 CPU:中央处理器,相当于人类的大脑,运算中心,控制中心. 内存:暂时储存数据,与CPU交互,8G,16G,32G,64G § 优点:读取速度快. § 缺点:容量小,造价高,断电即消失 ...


