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 ...
随机推荐
- (三)类数组对象 NamedNodeMap简单介绍
Ele.attrbutes将返回一个NamedNodeMap对象,即NamedNodeMap存储的是元素的“特性Attribute”集合.而集合中的每一个元素,都是Attr类型的对象. html: & ...
- Sublime Text3 配置 NodeJs 开发环境
题外话:使用visual studio开发NodeJs也是很方便,只需要安装插件即可. 本着对Sublime Text3的喜爱,尤其是最近更新后,界面和功能上感觉更nice了,那就配置一发环境吧! ( ...
- Mermaid js与流程图、甘特图..
https://mermaidjs.github.io/gantt.html https://github.com/jdbranham/grafana-diagram 用 mermaid 画甘特图 h ...
- React 避免重渲染
组件的重新渲染 我们可以在 React 组件中的 props 和 state 存放任何类型的数据,通过改变 props 和 state,去控制整个组件的状态.当 props 和 state 发生变化时 ...
- Linux 系统命令行入门基础
Linux 命令行组成结构 打包及压缩命令 tar 解压压缩包:
- spring boot中常用的配置文件的重写
@Configuration public class viewConfigSolver extends WebMvcConfigurerAdapter { /* spring boot 已经自动配置 ...
- Java权限访问修饰符
私有的,以 private 修饰符指定,在同一类内可见. 默认的,也称为 default,在同一包内可见,不使用任何修饰符. 受保护的,以 protected 修饰符指定,对同一包内的类和所有子类可见 ...
- Vue之变量、数据绑定、事件绑定使用举例
vue1.html <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/19 ...
- Python 第三方库 cp27、cp35 等文件名的含义(转)
转自 https://blog.csdn.net/lanchunhui/article/details/62417519 转自 https://stackoverflow.com/questions/ ...
- C#中decimal,double和float的区别
float 单精度浮点 32bit,double 双精度浮点64bit,decimal是高精度 128bit,浮点型.float double 是 基本类型(primitive type),decim ...