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

心不动则不痛 提交于 2020-05-04 04:42:04

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

提交代码

 

这答题主要是理解题意,For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.这句话一直琢磨不透,对于任意一个节点,所有的从这个节点到子节点们的简单路径都包括相同数目的黑色节点。这个路径是指节点到叶子结点的所有路径。

过去用的递归都是向下传值,这个递归用法是向上传值,向上传值的递归有一些规范:

  1.返回值是数字,向下传递时,传参要写在函数里,而向上传参是将值放在返回值里。

  2.向上传递,首先要明确最底层返回信息和条件。

  3.获得从下面传上来的参数后,要做什么条件判断,以及进行下一步操作,进一步返回参数。

  4.同时可以判断返回参数来判断成功失败。

 

#include<bits/stdc++.h>

using namespace std;

int N,M;

struct Node {
    int color;
    int value;
    Node* left;
    Node* right;
    //int cnt;
};

int FLAG = 1;
vector<Node*> nodVec, nodVec2;

void preOrder(Node* node) {
    //cout<< node->value<< endl;
    if(!FLAG) return;
    if(node->color == -1) {
        if(node->left != NULL && node->left->color == -1) {
            FLAG = 0;
            return;
        }
        if(node->right != NULL && node->right->color == -1) {
            FLAG = 0;
            return;
        }
    }
    if(node->left != NULL) {
        preOrder(node->left);
    }
    if(node->right != NULL) {
        preOrder(node->right);
    }
}

int dfs(Node* node) {

    if(node == NULL) return 1;

    int l = dfs(node->left);
    int r = dfs(node->right);

    if(l == -1 || r == -1) {
        return -1;
    }

    if(l == r) {
        if(node->color == 0) {
            return l + 1;
        }
        else {
            return l;
        }
    }
    else {
        return -1;
    }
}

int main() {
    cin>> N;
    for(int i = 0; i < N; i++) {
        cin>>M;
        int v;
        int flag = 1;
        Node* head = new Node();
        for(int j = 0; j < M; j++) {
            Node* node = new Node();
            scanf("%d", &v);
            if(v > 0) {
                node->value = v;
            }
            else {
                node->color = -1;
                node->value = -v;
            }
            //cout<< node->value<<endl;
            if(j == 0) {
                head = node;
                //cout<< head->value<< endl;
                if(v < 0) {
                    flag = 0;
                }
            }
            else {
                //cout<< head->value<< endl;
                Node* node2 = head;
                while(1) {
                    if(node->value < node2->value) {
                        if(node2->left == NULL) {
                            node2->left = node;
                            break;
                        }
                        else {
                            node2 = node2->left;
                        }
                    }
                    else {
                        if(node2->right == NULL) {
                            node2->right = node;
                            break;
                        }
                        else {
                            node2 = node2->right;
                        }
                    }
                }
            }
        }
        if(!flag) {
            printf("No\n");
        }
        else {
            FLAG = 1;
            preOrder(head);
            if(!FLAG) {
                printf("No\n");
            }
            else {
                if(dfs(head) == -1) {
                    printf("No\n");
                }
                else {
                    printf("Yes\n");
                }
            }
        }
    }
    return 0;
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!