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
题意:给出n个二叉树的先序序列,让你判断该二叉树是否为一棵红黑树。
思路:根据红黑树的定义知,红黑树有如下性质:
①根结点一定是黑色
②任意结点其左子树到叶结点和右子树到叶结点,所包含的黑色结点数相同。(注意是任意节点,而不仅仅是根结点到叶结点)
③任意红色结点,其孩子结点一定是黑色。
根据以上性质,进行判断。首先定义二叉树节点
struct node{
int data,numBlack,lBlack,rBlack; //numblack为当前结点到叶结点的黑色结点计数,lblack和rblack分别为左右子树到叶结点黑色结点计数
node* lchild;
node* rchild;
node(int d,int lb=0,int rb=0):data(d),lBlack(lb),rBlack(rb),lchild(NULL),rchild(NULL){}
};
由于红黑树是一棵BST树,所以根据BST树的性质(左子树<=根节点值,右子树>根节点值),进行建树,建树完毕后,再通过后序遍历,判断每个结点是否满足条件②和③。
特别提醒:刚开始我是通过先序序列和中序序列(先序序列排序后得到)进行建树,但发现case2始终出现:段错误。后来才明白,所给出的数据可能含有相同数据结点,此时,通过先序和中序序列是无法唯一确定一棵树的,而通过直接建立BST树的方式则可以唯一建树。
参考代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=35;
int n;
struct node{
int data,numBlack,lBlack,rBlack;
node* lchild;
node* rchild;
node(int d,int lb=0,int rb=0):data(d),lBlack(lb),rBlack(rb),lchild(NULL),rchild(NULL){}
};
void insert(node*& root,int x){
if(root==NULL){
root=new node(x);
return;
}else if(abs(x)<=abs(root->data)) insert(root->lchild,x);
else insert(root->rchild,x);
}
void postOrder(node*& root,bool& flag){
if(root==NULL) return;
postOrder(root->lchild,flag);
postOrder(root->rchild,flag);
if(root->data>0){ //黑色结点
if(root->lchild==NULL) root->lBlack=1; //若左孩子为空,则令当前结点的lblack=1
else root->lBlack=root->lchild->numBlack+1; //否则lblack=左孩子的lblack+1
if(root->rchild==NULL) root->rBlack=1; //右孩子同理
else root->rBlack=root->rchild->numBlack+1;
}else{ //红色结点
if(root->lchild==NULL) root->lBlack=0; //若左孩子为空,令当前结点lblack=0
else root->lBlack=root->lchild->numBlack; //否则lblack=左孩子的lblack
if(root->rchild==NULL) root->rBlack=0; //右孩子同理
else root->rBlack=root->rchild->numBlack;
if(root->lchild!=NULL&&root->lchild->data<0) flag=false; //若当前节点为红色并且左孩子存在且也是红色,则flag=false
if(root->rchild!=NULL&&root->rchild->data<0) flag=false; //若当前节点为红色并且右孩子存在且也是红色,则flag=false
}
root->numBlack=max(root->lBlack,root->rBlack); //更新根结点的黑色结点计数
if(root->lBlack!=root->rBlack) flag=false; //若左右孩子黑色结点个数不同,则flag=false
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++){
int num,t;
bool flag=true; //flag标记是否为红黑树
node* root=NULL;
scanf("%d",&num);
for(int j=0;j<num;j++){
scanf("%d",&t);
insert(root,t);
}
postOrder(root,flag);
if((root->data<0||!flag)) printf("No\n");
else printf("Yes\n");
}
return 0;
}