1. 程式人生 > 實用技巧 >pat 1135

pat 1135

1135Is It A Red-Black Tree(30分)

There is a kind of balanced binary search tree namedred-black treein 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 17

Sample Output:

Yes
No
No

題意:判斷一顆給定的樹是否是紅黑樹,紅黑樹需要滿足以下條件:

  1.每個結點要麼是紅色要麼是黑色

  2.根節點是黑色的

  3.每個葉子結點是黑色的。紅黑樹裡面的葉子結點表示的是最下層的空結點

  4.如果一個結點是紅色的,那麼它的兩個孩子結點都是黑的。

  5.從每一個結點出發,到任意葉子節點的簡單路徑上所包含的黑色結點個數相同。

思路:題目給定了前序遍歷序列,需要注意的是紅黑樹是一個特殊的二叉排序樹,它也符合左子樹的值<根結點的值<右子樹的值 的特點,因此可以根據前序遍歷建立一顆二叉查詢樹,然後判斷這顆樹是否為紅黑樹。

程式碼如下:

#include<cstdio>
#include<cstdlib>
#include<math.h>
#include<vector>
using namespace std; 
typedef struct node{
    int val;
    node* l;
    node* r;    
}node;
void judge2(node* root,int& mark){
    if(root->val<0){
        if(root->l!=NULL){
            if(root->l->val<0){
                mark=0;
            }
        }
        if(root->r!=NULL){
            if(root->r->val<0)
                mark=0;
        }
    }
    if(root->l!=NULL){
        judge2(root->l,mark);
    }    
}
void judge3(node* root,int count,vector<int> &v){
    if(root==NULL){
        v.push_back(count);
    }
    else{
        if(root->val>=0)
            count++;
        judge3(root->l,count,v);
        judge3(root->r,count,v);
        
    }
}
void judge(node* root){
    if(root->val<0){
        printf("No\n");
        return;
    }
    int mark=1;
    judge2(root,mark);
    if(mark==0){
        printf("No\n");
        return;
    }
    int count=0;
    vector<int> v;
    judge3(root,count,v);
    int sum=v[0];
    for(vector<int>::iterator it=v.begin();it!=v.end();it++){
        if(*it!=sum){
            printf("No\n");
            return;
        }
    }
    printf("Yes\n");
    return;
}
node* build(node* root,int val){
    if(root==NULL){
        root =(node*)malloc(sizeof(node));
        root->val=val;
        root->l=NULL;
        root->r=NULL;
         root;
    }
    else if(abs(val)<=abs(root->val)){
         root->l=build(root->l,val);
    }
    else
         root->r=build(root->r,val);
    return root;
}
int main(){
    int n,num,temp;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        node* root=NULL;
        scanf("%d",&num);
        for(int j=0;j<num;j++){
            scanf("%d",&temp);
            root=build(root,temp);
        }
        judge(root);
    }
    return 0;
}