【PAT】A1135 Is It A Red-Black Tree 【树】

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 17

Sample Output:

Yes
No
No

题意

给出K组数据。每组数据是一棵二叉树的先序遍历(正数代表黑色结点,负数代表红色结点),要求判断该树是否是红黑树(即满足题目所给的性质)。

思路

对于一个先序遍历,第一个元素是根结点,从第二个元素开始第一个比根结点大的元素及它的右边是右子树,它的左边是左子树(这里说的是绝对值)。由此递归建树。对于性质5,我们只需要检查根结点即可。因为它具有向上传递的性质。我们找出任意一条从根结点到叶子结点的路径的黑色结点个数,然后dfs+回溯来检验所有的性质。这里的难点是利用回溯来记录每条从根结点到叶子结点的黑色结点个数,**如果不回溯,会出错。**一开始写错了代码,但是这题由于只需要输出YES和NO,所以确定N的个数之后呢,答案有2^N种可能,对于前4组数据,N<=5。实际上可以逆向推出答案。

代码

#include <cstdio>
#include <map>
#include <algorithm>
#include <stack>
#include <vector>
#define RED 0
#define BLACK 1
#define MAX_N 30
using namespace std;
int preOrder[MAX_N];
struct Node{
    Node*left, *right;
    int value;
    int color;
    Node(int value) : left(NULL), right(NULL){
        if(value > 0){
            this->value = value;
            color = BLACK;
        }else{
            this->value = -value;
            color = RED;
        }
    };
}*root;
int abs(int t){
    return t > 0 ? t : -t;
}
// 递归建树
Node* build(int start, int end){
    if(start >= end) return NULL;
    Node* root = new Node(preOrder[start]);
    int i = start + 1;
    while (i < end && abs(preOrder[i]) < root->value) {
        i++;
    }
    root->left = build(start + 1, i);
    root->right = build(i, end);
    return root;
}


int black = 0, cnt = 0;
bool flag;
// 先找到一条路径
void getBlack(Node*root){
    if(root == NULL) return;
    black += root->color;
    getBlack(root->left);
}
// 返回颜色
int color(Node*x){
    return x == NULL ? BLACK : x->color;// 空结点认为是黑色结点
}
// 递归检查
void dfs(Node*root){
    if(root == NULL){
       if(cnt != black){// 不满足性质5
            flag = false;
        }
        return;
    }
    if(root->color == RED){// 检验性质4
        if(color(root->left) == RED) flag = false;
        if(color(root->right) == RED) flag = false;
    }
    if(root->color == BLACK) cnt++;// 累计黑色结点个数
    dfs(root->left);
    dfs(root->right);
    if(root->color == BLACK) cnt--;// 回溯
}
bool check(Node*root){
    if(root->color == RED) return false;// 检验性质2
    flag = true;
    black = 0;
    cnt = 0;
    getBlack(root);
    dfs(root);
    return flag;
}
int main() {
    int K;
    scanf("%d", &K);
    while (K-- > 0) {
        int N;
        scanf("%d", &N);
        for(int i = 0; i < N; i++){
            scanf("%d", &preOrder[i]);
        }
        
        root = build(0, N);
        printf(check(root) ? "Yes\n" : "No\n");

    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值