PAT Advanced 1135 Is It A Red-Black Tree (30) [红⿊树]

题目

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:
39
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. 判断是否为红黑树
    2.1 判断根节点是否为黑色
    2.2 判断每个节点到其子树叶子节点的黑色结点数相同
    2.3 判断红色节点的左右子节点都为黑色

Code

Code 01

#include <iostream>
#include <vector> 
using namespace std;
struct node {
    int v;
    node * f;
    node * r;
    node (){}
    node (int _v):v(_v){
        f=r=NULL;
    }
};
vector<int> pre;
// 前序建树 
node * create(int preL,int preR){
    if(preL>preR)return NULL;
    node * root = new node(pre[preL]);
    int k=preL+1;
    while(k<preR&&abs(pre[k])<=abs(pre[preL]))k++;//找第一个大于根节点的值 
    root->f=create(preL+1,k-1);
    root->r=create(k,preR);
    return root;
}
// 判断红色节点的两个孩子是否都是黑色节点 
bool judge1(node * root) {
    if(root==NULL)return true;
    if(root->v<0) {
        if(root->f!=NULL&&root->f->v<0)return false;
        if(root->r!=NULL&&root->r->v<0)return false;
    }
    return judge1(root->f)&&judge1(root->r);
}
// 获取当前节点高度(高度指:从当前节点到其子树的叶子节点的黑色结点数) 
int getNum(node * root) {
    if(root==NULL)return 1;
    int f = getNum(root->f);
    int r = getNum(root->r);
    return root->v>0?max(f,r)+1:max(f,r);
}
// 判断每个节点到其子树的叶子节点的黑色节点数相同 
bool judge2(node * root) {
    if(root==NULL) return true;
    int f= getNum(root->f);
    int r= getNum(root->r);
    if(f!=r)return false;
    return judge2(root->f)&&judge2(root->r);
}
int main(int argc,char * argv[]) {
    int k,n;
    scanf("%d",&k);
    for(int i=0; i<k; i++) {
        scanf("%d",&n);
        pre.clear();
        pre.resize(n);
        for(int j=0; j<n; j++) {
            scanf("%d",&pre[j]);
        }
        node * root=create(0,n-1);
        if(root->v<0||judge1(root)==false||judge2(root)==false) {
            printf("No\n");
        }else printf("Yes\n");
    }
    return 0;
}

Code 02

#include <iostream>
using namespace std;
struct node {
    int v;
    node * f;
    node * r;
    node (){}
    node (int _v):v(_v){
        f=r=NULL;
    }
};
// 前序建树 
void insert(node * &root,int v) {
    if(root==NULL) {
        root=new node(v);
        return;
    }
    if(abs(v)<=abs(root->v))
        insert(root->f, v);
    else
        insert(root->r,v);
}
// 判断红色节点的两个孩子是否都是黑色节点 
bool judge1(node * root) {
    if(root==NULL)return true;
    if(root->v<0) {
        if(root->f!=NULL&&root->f->v<0)return false;
        if(root->r!=NULL&&root->r->v<0)return false;
    }
    return judge1(root->f)&&judge1(root->r);
}
// 获取当前节点高度(高度指:从当前节点到其子树的叶子节点的黑色结点数) 
int getNum(node * root) {
    if(root==NULL)return 1;
    int f = getNum(root->f);
    int r = getNum(root->r);
    return root->v>0?max(f,r)+1:max(f,r);
}
// 判断每个节点到其子树的叶子节点的黑色节点数相同 
bool judge2(node * root) {
    if(root==NULL) return true;
    int f= getNum(root->f);
    int r= getNum(root->r);
    if(f!=r)return false;
    return judge2(root->f)&&judge2(root->r);
}
int main(int argc,char * argv[]) {
    int k,n;
    scanf("%d",&k);
    for(int i=0; i<k; i++) {
        scanf("%d",&n);
        int nds[n];
        node * root=NULL;
        for(int j=0; j<n; j++) {
            scanf("%d",&nds[j]);
            insert(root,nds[j]);
        }
        if(root->v<0||judge1(root)==false||judge2(root)==false) {
            printf("No\n");
        }else printf("Yes\n");
    }
    return 0;
}


猜你喜欢

转载自www.cnblogs.com/houzm/p/12341320.html