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

22 篇文章 0 订阅
22 篇文章 0 订阅

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. 所有叶子都是黑色(叶子是NIL节点)
4. 每个红色节点的两个子节点都是黑色(从每个叶子到根的所有路径上不能有两个连续的红色节点)

5. 从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点。

本来红黑树的性质还算好理解,但是这个题的图没有给清楚,导致一些人对性质3有所误解。


可以看一下上边这个图,注意说红黑树的时候,叶子节点指的是NIL节点,而不是我们平时理解的没有孩子的节点!

说一下这个题可能的坑点吧:

1.这个题并不让你真的建立一棵红黑树或者AVL(否则所有人都会疯掉的),而是给你一个前序序列,建一棵BST,BST是非常好建立的。

2.由于要区分红色和黑色节点,所以输入序列中小于0的值代表红色,大于0的值代表黑色。我的做法是Node结构体里额外定义一个color变量用来区分红色和黑色。

3.那么问题来了,红黑树的第一个性质是“节点要么是红色要么是黑色”,这个怎么判断呢?如果输入序列里有个值是0,那么它到底是红色还是黑色?根据我的尝试,测试数据没有这个情况,是我想多了。。。

4.如果你对性质3理解有所误区,就是你误以为红黑树中的叶子节点是没有孩子的节点,那么你只能得21分,最后两个测试点错误

5.性质1和3不用判断;性质2判断很简单;性质4写个递归,别写炸了;性质5有个非常快的小技巧,并不用把每个节点到这棵子树的所有叶子路径都判断一遍,只要满足前边4个性质,只需判断根节点是否满足性质5即可。(我不是太理解,但这么做确实是对的)。如果你把每个节点是否满足性质5都判断一遍也能AC,数据范围比较小,而且也慢不了多少。

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>

using namespace std;

typedef struct node {
    int val;
    int color;  // 定义0是红色 1是黑色
    struct node *left;
    struct node *right;
} Node;

void insert( Node *&root, int val, int color ) {
    if( root == NULL ) {
        root = ( Node* )malloc( sizeof( Node ) );
        root->left = root->right = NULL;
        root->val = val;
        root->color = color;
        return;
    }
    else if( root->val < val ) insert( root->right, val, color );
    else insert( root->left, val, color );
}

Node* createBST( int n ) {
    int val;
    int color = 0;
    Node *root = NULL;
    for( int i = 0; i < n; i++ ) {
        scanf( "%d", &val );
        if( val < 0 ) insert( root, -val, 0 );
        else insert( root, val, 1 );
    }

    return root;
}

// 判断性质2
bool judgeRoot( Node* root ) {
    return root->color ? true : false;
}

// 判断性质4
bool judgeColor( Node* root ) {
    if( root == NULL ) return true;
    if( !root->color ) {
        if( root->left != NULL && root->left->color == 0 ) return false;
        if( root->right != NULL && root->right->color == 0 ) return false;
    }
    return judgeColor( root->left ) && judgeColor( root->right );
}

// 判断性质5
// 注释这部分是我把每个节点是否满足性质5都判断了一遍
// 用这个提交也能AC
/*
vector<int> vec;
void dfs( Node* root, int cur ) {
    if( root == NULL ) {
        vec.push_back( cur + 1 );
        return;
    }
    cur = cur + root->color;
    dfs( root->left, cur );
    dfs( root->right, cur );
}

bool judgePath( Node* root ) {
    if( root == NULL ) return true;
    //printf( "开始搜索%d节点\n", root->val );
    vec.clear();
    dfs( root, 0 );

    int pre = -1;
    for( int i = 0; i < vec.size(); i++ ) {
        if( pre != -1 && pre != vec[i] ) return false;
        pre = vec[i];
    }

    return judgePath( root->left ) && judgePath( root->right );
}
*/

vector<int> vec;
void dfs( Node* root, int cur ) {
    if( root == NULL ) {
        // 就是这个地方,如果当前节点是NULL
        // 说明已经到叶子节点了,因为红黑树的叶子节点是黑色的,所以要加上1
        // (因为我定义黑色节点的color值=1)
        // 我们平时理解的叶子节点是这个情况
        // if( root->left == NULL && root->right == NULL )
        // 如果你按上边这个判断,那么就只能得21分
        vec.push_back( cur + 1 );
        return;
    }
    cur = cur + root->color;
    dfs( root->left, cur );
    dfs( root->right, cur );
}

// 判断性质5
// 只判断根节点是否满足性质5即可
// 判断的方法是去计算每条路的黑色节点个数
bool judgePath( Node *root ) {
    vec.clear();
    dfs( root, 0 );
    int pre = -1;
    for( int i = 0; i < vec.size(); i++ ) {
        if( pre != -1 && pre != vec[i] ) return false;
        pre = vec[i];
    }
    return true;
}

int main() {
    int k, n;
    scanf( "%d", &k );

    while( k-- ) {
        scanf( "%d", &n );
        Node *root = createBST( n );
        if( judgeRoot( root ) && judgeColor( root ) && judgePath( root ) ) {
            printf( "Yes\n" );
        }
        else printf( "No\n" );
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值