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、关于建立原树,有两种办法。一是,由于搜索树的中序是从小到大的有序序列,可以先将所有节点排序得到中序序列。再按照已知前序和中序的方法,建立二叉树。二是,由于有序二叉树的先序序列的意义:根在前子树在后,且小于根的节点在左,大于的在右。所以可以直接把先序序列当作有序二叉树的插入的顺序,按顺序插入节点,得到原树。注意已知序列是有序二叉树的先序,则可以把它当作插入顺序。但已知插入顺序,这个插入顺序却不一定是先序。