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的更多相关文章

  1. PAT A1135 Is It A Red Black Tree

    判断一棵树是否是红黑树,按题给条件建树,dfs判断即可~ #include<bits/stdc++.h> using namespace std; ; struct node { int ...

  2. [转载] 红黑树(Red Black Tree)- 对于 JDK TreeMap的实现

    转载自http://blog.csdn.net/yangjun2/article/details/6542321 介绍另一种平衡二叉树:红黑树(Red Black Tree),红黑树由Rudolf B ...

  3. 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 ...

  4. Red Black Tree 红黑树 AVL trees 2-3 trees 2-3-4 trees B-trees Red-black trees Balanced search tree 平衡搜索树

    小结: 1.红黑树:典型的用途是实现关联数组 2.旋转 当我们在对红黑树进行插入和删除等操作时,对树做了修改,那么可能会违背红黑树的性质.为了保持红黑树的性质,我们可以通过对树进行旋转,即修改树中某些 ...

  5. CF1208H Red Blue Tree

    CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有 ...

  6. 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 ...

  7. 计蒜客 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 ...

  8. Red Black Tree(红黑树)

    (修改于 2018-05-06 15:53:22 还差删除维护操作.层序遍历没完成.维护操作没完成不想写层序遍历怎么办...) 今天下午完成了红黑树的插入的维护操作,但删除的维护操作还没有解决,删除的 ...

  9. ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

    题意:一棵树上有m个红色结点,树的边有权值.q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化.q次查询相互独立. 分析:数 ...

  10. Red Black Tree java.util.TreeSet

    https://docs.oracle.com/javase/9/docs/api/java/util/SortedMap.html public interface SortedMap<K,V ...

随机推荐

  1. class面向对象-1

    一.基本定义 class cl(object): def __init(self,var) self.var=var def func(self,i) print('%s is in %s'%(i,s ...

  2. js 首次进入弹窗

    今天有个需求,首次进入需要弹窗,然后就在网上找了下,虽然看了很多但是说的都不是我想要的,最后终于到了一个合适的. function get_cookie(Name) { var search = Na ...

  3. python3 自动识图

    一.安装依赖库 pip install pytesseract pip install pillow 二.安装识图引擎tesseract-ocr https://pan.baidu.com/s/1Qa ...

  4. 莫烦sklearn学习自修第八天【过拟合问题】

    1. 什么是过拟合问题 所谓过拟合问题指的是使用训练样本进行训练时100%正确分类或规划,当使用测试样本时则不能正确分类和规划 2. 代码实战(模拟过拟合问题) from __future__ imp ...

  5. volatile和synchronized的区别

    volatile和synchronized特点 首先需要理解线程安全的两个方面:执行控制和内存可见. 执行控制的目的是控制代码执行(顺序)及是否可以并发执行. 内存可见控制的是线程执行结果在内存中对其 ...

  6. jqGrid选中行、格式化、自定义按钮、隐藏

    获取选择一行的id: var id=$('#jqGrid').jqGrid('getGridParam','selrow'); 获取选择多行的id: var ids=$('#jqGrid').jqGr ...

  7. Previous Workflow Versions in Nintex Workflow

    Previous Workflow Versions in Nintex Workflow September 4, 2013 It occurred to me that even though I ...

  8. THEPYTHONCHALLENG闯关记录

    由于是自己看视频学python,总觉得不写几行代码就什么都没有学到. 找了一个写代码的网站其实只是因为这个看起来好玩. 闯关地址http://www.pythonchallenge.com/index ...

  9. [Codeforces266E]More Queries to Array...——线段树

    题目链接: Codeforces266E 题目大意:给出一个序列$a$,要求完成$Q$次操作,操作分为两种:1.$l,r,x$,将$[l,r]$的数都变为$x$.2.$l,r,k$,求$\sum\li ...

  10. Android 右上角菜单栏

    1 创建菜单栏 在res下新建menu文件夹,并且创建righttopmenu.xml righttopmenu.xml: <?xml version="1.0" encod ...