1. 程式人生 > >1135. Is It A Red-Black Tree (30)

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

時間 table i++ 用法 ++ link 代碼 positive arc

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;
}

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