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.
|
|
|
|
|
|
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 17Sample Output:
Yes No No
队友很厉害。。。
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
int flag=0;
struct node {
int num;
struct node *left,*right;};
struct node*creat(struct node * now ,int num)
{
if (now==NULL)
{
now=new struct node ();
now->num=num;
now->left=NULL;
now->right=NULL;
}
else if (fabs(num)<=fabs(now->num))
now->left=creat(now->left,num);
else if (fabs(num)>fabs(now->num))
now->right=creat(now->right,num);
return now;
}
int pd2(struct node*now )
{
return now->num>0;
}
int pd4(struct node *now )//判断第四条原则,是不是红色的孩子都是黑的
{
queue <struct node *> q;
q.push(now);
while(!q.empty())
{
struct node *temp=q.front();
q.pop();
int num=temp->num;
if (num<0)
{
if(temp->left)
if(temp->left->num<0) return 0;
if(temp->right)
if(temp->right->num<0) return 0;
}
if (temp->left) q.push(temp->left);
if (temp->right) q.push(temp->right);
}
return 1;
}
int pd5(struct node *now)//判断节点到每个叶子的路径所过的黑色节点数是否相等
{
if (now==NULL) return 0;
int le=pd5(now->left);
int ri=pd5(now->right);
if(le!=ri) flag=0;
if(now->num>0)
return max(le,ri)+1;
else
return max(le,ri);
}
int main()
{
int n;
cin>>n;
while(n--)
{
int t;
cin>>t;
struct node *head=NULL;
for (int i=0;i<t;i++)
{
int num;
cin>>num;
head=creat(head,num);
}
if (!pd2(head))
cout<<"No"<<endl;
else
{
if(!pd4(head))
cout<<"No"<<endl;
else
{
flag=1;
pd5(head);
if (!flag)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
}
}
}
return 0;
}
以下是利用深搜求解
#include<iostream>
#include<cmath>
using namespace std;
struct node{
int num;
struct node *left,*right;
};
int flag=1,flag2=1;
struct node *creat(struct node *now,int num)
{
if (now==NULL)
{
now =new struct node();
now->num=num;
now->left=NULL;
now->right=NULL;
return now;
}
else
{
if (fabs(num)<fabs(now->num))
now->left=creat(now->left,num);
else
now->right=creat(now->right,num);
return now;
}
}
void dfs(struct node *now ,bool father)
{
if (now==NULL)
return ;
else
{
if (now->num<0&&!father) flag=0;
dfs(now->left,now->num>0);
dfs(now->right,now->num>0);
}
}
int pd5(struct node *now)//判断节点到每个叶子的路径所过的黑色节点数是否相等
{
if (now==NULL) return 0;
int le=pd5(now->left);
int ri=pd5(now->right);
if(le!=ri) flag2=0;
if(now->num>0)
return max(le,ri)+1;
else
return max(le,ri);
}
int main()
{
int n;
cin>>n;
while(n--)
{
int t;
cin>>t;
struct node *head=NULL;
for (int i=0;i<t;i++)
{
int num;
cin>>num;
head=creat(head,num);
}
if (head->num<0)
cout<<"No"<<endl;
else
{
flag=1;
dfs(head,1);
if (!flag)
cout<<"No"<<endl;
else
{
flag2=1;
pd5(head);
if (!flag2)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
}
}
}
return 0;
}