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 ...
随机推荐
- 重启iis命令
iisreset
- elasticsearch的映射
一.简介: 映射:在创建索引时,可以预先定义字段的类型(映射类型,也就是type,一个索引可以有一个或多个类型)及相关属性. Elasticsearch会根据JSON源数据的基础类型猜测你想要的字段映 ...
- zabbix模板
https://github.com/xm-y/zabbix-community-repos https://monitoringartist.github.io/zabbix-searcher/
- PHPStorm从入门到精通
1. 使用phpstorm+xdebug进行调试 首先,安装php的xdebug扩展 查看phpinfo中php的版本,php的安装位数,php的是否线程安全:根据这些下载对应的xdebug.dll ...
- pip 安装 MySQL-python 报错
报错一:EnvironmentError: mysql_config not found 解决:yum install mysql-devel 报错二:Python.h No such file or ...
- js正則表達式
正則表達式實例化的兩種方式: 字符型 var a=// 對象型var a=new RegExp(,) 修飾符: i:忽略大小寫 g:全局搜索 m:多行搜索 元字符: \轉義字符 \w:字符,數字,下劃 ...
- python排序 sorted()与list.sort() (转)
该文章为转载:原文地址为:https://www.cnblogs.com/zuizui1204/p/6422939.html 只要是可迭代对象都可以用sorted . sorted(itrearble ...
- 【python练习题】程序16
#题目:输出指定格式的日期. import time print (time.strftime('%Y:%m:%d %X',time.localtime(time.time())))
- Overrid Equals Defined Operator
public class Common { public override int GetHashCode() { return base.GetHashCode(); } public overri ...
- Express static 托管静态文件 理解
今天偶尔看了一下服务端渲染,遇到了express.static, 在以前学习webpack配置服务端渲染时,也使用express.static 中间件,两者配置不太一样,由于当时也没有认真学,所以 一 ...


