【PAT 1135. Is It A Red-Black Tree (30)】& 二叉树
来源:互联网 发布:梦幻手游宝宝评分算法 编辑:程序博客网 时间:2024/06/08 18:25
- 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;}
- PAT 1135. Is It A Red-Black Tree (30) 二叉搜索树建立 + 红黑树判断
- 【PAT 1135. Is It A Red-Black Tree (30)】& 二叉树
- PAT甲级 1135. Is It A Red-Black Tree (30)
- PAT-1135 Is It A Red-Black Tree(二叉查找树的创建和遍历)
- 1135. Is It A Red-Black Tree (30)-PAT甲级真题
- PAT甲级1135 Is It A Red-Black Tree
- 1135. Is It A Red-Black Tree (30)
- 1135. Is It A Red-Black Tree (30)
- 1135. Is It A Red-Black Tree (30)[红黑树判断]
- 1135. Is It A Red-Black Tree (30) 红黑树
- 1135. Is It A Red-Black Tree (30)
- 1135. Is It A Red-Black Tree (30)
- PAT (Advanced Level) Practise 1135Is It A Red-Black Tree (30)
- pat1135 Is It A Red-Black Tree (30)(红黑树)
- 1135. Is It A Red-Black Tree (30)(判断红黑树)
- PAT 1123. Is It a Complete AVL Tree (30) 平衡树构建+ 完全二叉树判断
- pat甲1123. Is It a Complete AVL Tree (AVL树+层次遍历+完全二叉树)
- 1043. Is It a Binary Search Tree (25)【二叉树】——PAT (Advanced Level) Practise
- spring学习之配置文件
- 各大技术团队网站
- 比特币历史数据
- android 自定义view的流程
- kubernets部署(纯净版)
- 【PAT 1135. Is It A Red-Black Tree (30)】& 二叉树
- Linux下的packet socket使用总结
- ubuntu下安装mysql
- 轮播图的实现(下)
- 为什么JDK 1.8中不再有永久代(permanent generation)
- 频率调制
- python(4)
- CSS3多个背景图片和之前背景的笔记
- Navicat for MySQL破解,以及连接数据库出现错误:1045-Access denied for user 'root'@'localhost'解决方法