pat 甲级 1135. Is It A Red-Black Tree (30)

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

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

题意:判断是否是红黑树。
红黑树定义:1:每个节点要么红要么黑2:根节点一定是黑的3:红色节点的子节点都是黑色4:任意一个节点的左右子树上的黑色节点数相同,其实就是根节点到叶子节点的所有路径上的黑色节点个数相同即可。
思路:已给出前序遍历,按顺序插入二叉树,构建搜索树。之后判断是否满足红黑树的定义。即对2,3,4三点定义进行判断。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 30000+5
typedef long long ll;
struct Node {
    int key;
    Node* left, *right;
};
Node *NIL,*root;
int t,n;
void insert(Node* &root,int key) {
    if (root == NIL) {
        root = new Node;
        root->key = key;
        root->left = root->right = NIL;
        return;
    }
    if (abs(key) > abs(root->key))
        insert(root->right, key);
    else
        insert(root->left, key);
}
bool is_correct = 1;
bool is_first = 0; int tot_num = 0;
void judge(Node* root,int num) {
    if (root == NIL) { 
        if (!is_first) {
            tot_num = num;
            is_first = 1;
        }
        else if (num != tot_num)is_correct = false;
        return; 
    }
    if (root->key < 0) {//当前节点是红色
        if ((root->left != NIL&&root->left->key < 0) || (root->right != NIL&&root->right->key < 0)) {
            is_correct = false; return;
        }
        judge(root->left,num);
        judge(root->right,num);
    }
    else {//当前黑色节点,num数量加1
        judge(root->left, num+1);
        judge(root->right, num+1);
    }
}

int main() {
    scanf("%d", &t);
    while (t--) {
        root = NIL; is_first = 0,tot_num = 0,is_correct = true;
        scanf("%d",&n);
        for (int i = 0; i < n;i++) {
            int data; scanf("%d",&data);
            if (!i&&data < 0)is_correct = false;
            insert(root,data);
        }
        if (!is_correct) { printf("No\n"); continue; }
        judge(root, 0);
        if (is_correct)puts("Yes");
        else puts("No");
    }
}

 

posted on 2018-03-16 22:32  ZefengYao  阅读(164)  评论(0编辑  收藏  举报

导航