【PAT 1135. Is It A Red-Black Tree (30)】& 二叉树

来源:互联网 发布:梦幻手游宝宝评分算法 编辑:程序博客网 时间:2024/06/08 18:25
  1. 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)红节点的孩子节点为黑
4)每个节点的左右子树中黑色节点的个数相同(一个孩子节点对父节点的贡献为该节点的左子树上的黑色节点个数)

AC代码:

#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;const int MAX = 35;typedef long long LL;int l[MAX],r[MAX],a[MAX],ml[MAX],mr[MAX],ok;void bu(int x,int y){    if(abs(a[x]) >= abs(a[y])){        if(!l[x]) l[x] = y;        else bu(l[x],y);    }    else{        if(!r[x]) r[x] = y;        else bu(r[x],y);    }    return ;}int dfs(int x){    if(!ok) return 0;    if(l[x]) ml[x] += dfs(l[x]);    if(r[x]) mr[x] += dfs(r[x]);    if(ml[x] != mr[x]) ok = 0;    if(a[x] < 0) return ml[x];    else return ml[x] + 1;}int main(){    int T;    scanf("%d",&T);    while(T--){        memset(l,0,sizeof l),memset(r,0,sizeof r);        memset(ml,0,sizeof ml),memset(mr,0,sizeof mr);        int n;        scanf("%d",&n);        for(int i = 1; i <= n; i++)            scanf("%d",&a[i]);        if(a[1] < 0) {            puts("No");            continue;        }        for(int i = 2; i <= n; i++)            bu(1,i);        ok = 1;        for(int i = 1; i <= n; i++){            if(l[i])                if(a[i] < 0 && a[l[i]] < 0) ok = 0;            if(r[i])                if(a[i] < 0 && a[r[i]] < 0) ok = 0;        }        if(!ok) puts("No");        else{            dfs(1);            if(!ok) puts("No");            else puts("Yes");        }    }    return 0;}
阅读全文
1 0
原创粉丝点击