PAT 1135 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.

rbf1.jpgrbf2.jpgrbf3.jpg
Figure 1Figure 2Figure 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

首先拿到这题,读完题,想了很多处理几个条件的办法,先把叶子找出来放在一个数组里,在每个结点结构体内放叶子的数组,但是想找到这个叶子的路径上黑点的数目就得唯一标志这个叶子,首先建树然后层序给叶子标志,所以叶子数组内得放叶子的位置,然后再添加一个数目的,比较每个结点的数目......然后遍历的时候记录每个遍历到叶子的黑点数目而这个遍历得嵌套一个遍历所有叶子的条件......套太多了放弃了

后来又发现。建不出树来,只有先序遍历!其实这个时候是记得红黑树是自平衡二叉查找树的,但是就是.....没有把一直的信息想到并且应用啊,后来发现,题目中就已经给出a kind of balanced binary search tree,其实最近一直在调整,调整看题要仔细,但还是会漏信息....绝了

后来还是去看的题解,这里就直接写题解的解题思路了。

....可能是没有做过任意结点到叶子数目的题目,竟然想不到用递归....你要是让我求深度,求叶子balabala我肯定会用...思路更重要!寻找一个新的问题解得时候重要的是思路呀呀呀!!!

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<int> arr;
struct node {
    int val;
    struct node *left, *right;

};
node* build(node *root, int v) {
    if(root == NULL) {
        root = new node();
        root->val = v;
        root->left = root->right = NULL;
    } else if(abs(v) <= abs(root->val))
        root->left = build(root->left, v);
    else
        root->right = build(root->right, v);
    return root;
}

bool judge1(node *root) {
    if (root == NULL) return true;
    if (root->val < 0) {
        if (root->left != NULL && root->left->val < 0) return false;
        if (root->right != NULL && root->right->val < 0) return false;
    }
    return judge1(root->left) && judge1(root->right);
}

int getNum(node *root) {
    if (root == NULL) return 0;
    int l = getNum(root->left);
    int r = getNum(root->right);
    return root->val > 0 ? max(l, r) + 1 : max(l, r);
}

bool judge2(node *root) {
    if (root == NULL) return true;
    int l = getNum(root->left);
    int r = getNum(root->right);
    if(l != r) return false;
    return judge2(root->left) && judge2(root->right);
}

int main() {
    int k, n;
    scanf("%d", &k);
    for (int i = 0; i < k; i++) {
        scanf("%d", &n);
        arr.resize(n);
        node *root = NULL;
        for (int j = 0; j < n; j++) {
            scanf("%d", &arr[j]);
            root = build(root, arr[j]);
        }
        if (arr[0] < 0 || judge1(root) == false || judge2(root) == false)
            printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;

}

首先哭泣一下,别人家的代码非常优越了,逻辑清晰明了,运行时间都是0s

回想一下自己写的,基本都是1s,甚至有时候还到2s....生命不息哭泣不止啊,学习别人的代码,更要学习解题思路!

首先建树,思想固化在以前传数组,但是这个是不同的情况,只有一组数据,挨个传到函数里,如果当前数的绝对值大于根节点的就左子树递归,不然就是右子树递归。后来发现平衡二叉树就是这么建的...做题太少啊

再来看判断条件,虽然有五条但是我们可以简化成两个判断:

1.递归判断If a node is red, then both its children are black.就是我们这里的judge1函数,但是稍微做了一下改动,如果根节点是红色如果左子树不为空如果他的值小于零返回false,如果右子树不为空如果他的值小于零返回false,递归的往下走

return judge1(root->left) && judge1(root->right)

2.首先写一个获得一个结点黑色结点个数的函数,其实我们不断传入root->l,root->r的过程就是把所有结点递归了一遍,传入的root->l返回的num就是这个左子树递归到最后叶子的黑色结点数目,这里判断的时候并不是判断的叶子,而是判断的root->val > 0,因为有最后一个结点是红色的情况,就是第三条规则。我们的judge2里不断地传入root->left,root->right并比较此时的两个结点的黑色结点数目l,r是否相等,return judge2(root->left) && judge2(root->right)不断地递归。

啊思想很重要,就像求树的深度,其实和这个是一样的,返回这个函数,或者是+1;

继续奋斗呀!!!

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值