PAT 1135 Is It A Red-Black Tree 【C++版】
1.题意
给出一个先序序列,判断由这个先序序列得到的红黑树是否是一棵红黑树。其中红黑树需满足的定义如下:
(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.
可能对这里的第三点不好分析,其意思就是:将NULL
看成是一个叶子节点,这个节点是black。
2.分析
针对这题,在这里,我们需要判断如下几点即可:
- step1:根是否为空?
- step2:如果节点为red,那么其孩子结点是否为black?
- step3:每个节点到其子叶子节点上的black节点数目是否相同?
这里比较难实现的就是第三点,但是稍微思考一下,(需要抽象的能力!!抽象在计算机里真特么重要)不难发现,其实step3 <=>求该节点的高度值,这里的高度是只计算黑节点在内的高度
,然后比较左右两个子树的包含的黑色高度是否相同,如果相同则满足条件,否则不满足。
3.代码
#include<cstdio>
#include<algorithm>
#include<iostream>
#define maxn 35
using namespace std;
struct node{
node* lchild;
node* rchild;
int data;
int height;//高度【从下到上 root的高度最高】
int color;//表示节点颜色 0=black 1=red
};
int pre[maxn] ;
int in[maxn];
bool flag1;//路径中黑色节点数是否相同
bool flag2;//红色节点的children 是否都是黑色
node* create(node* &root, int v) {
if(root == NULL) {
root = new node();
root->data = abs(v);
if(v<0) root->color = 1;//red
else root->color = 0;//black
root->lchild = root->rchild = NULL;
} else if(abs(v) <= abs(root->data))
root->lchild = create(root->lchild, v);
else
root->rchild = create(root->rchild, v);
return root;
}
void inOrder(node* root){
if(root->lchild!=NULL) inOrder(root->lchild);
cout << root->data << " ";
if(root->rchild!=NULL) inOrder(root->rchild);
}
int getNum(node *root) {
if(root == NULL) return 0;
else if(root->color == 0){ //如果是black
return max(getNum(root->lchild),getNum(root->rchild)) + 1;//加1
}
else{
return max(getNum(root->lchild),getNum(root->rchild));//不加1
}
}
void judge2(node *root) {
if(!flag2) return;//直接返回
int lLen = getNum(root->lchild);
int rLen = getNum(root->rchild);
if(lLen - rLen != 0){
flag2 = false;
}
//不要忘记了!!!上述的过程只判断了root,还没有判断root的左右子树 【错过】
if(root->lchild != NULL && flag2) judge2(root->lchild);
if(root->rchild != NULL && flag2) judge2(root->rchild);
}
//判断这个二叉树的根是否是black
bool isBlackRoot(node* root){
if(root->color == 1) return false;
return true;
}
void judge1(node* root){
if(!flag1) return;
if(root->color == 1){//如果当前是红色的
if(root->lchild!=NULL && root->lchild->color == 1){//如果存在左子树,其为red
flag1 = false;
}
if(root->rchild!=NULL && root->rchild->color == 1){
flag1 = false;
}
}
if(root->lchild != NULL && flag1) judge1(root->lchild);//判断左子树
if(root->rchild != NULL && flag1) judge1(root->rchild);//判断右子树
}
int main(){
int K,N;
cin >> K ;
while(K--){
int i,j;
cin >>N;
fill(pre,pre+N,0);//重置为0
flag1 = true;
flag2 = true;
node* root = NULL;
for(i = 0;i< N;i++){
cin >> pre[i];
in[i] = abs(pre[i]);
root = create(root,pre[i]);
}
judge1(root);
judge2(root);
if(flag2){
if(isBlackRoot(root) && flag1)
cout <<"Yes\n";
else
cout <<"No\n";
}
else{
cout <<"No\n";
}
}
}
4.测试用例
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
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
2
8
10 -7 5 -6 8 15 11 17
8
10 -7 5 -6 8 -15 11 17
1
8
10 -7 5 -6 8 -15 11 17
1
2
1 2
5.执行结果
这题交了好几次,其中的有几次还有段错误。。。(⊙︿⊙)!