我先在CSDN上面发表了同样的文章,见https://blog.csdn.net/weixin_44385565/article/details/88863693 排版比博客园要好一些。。

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

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.

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:


 -   - -   -

 -  -  -   -

 -  -   - 

Sample Output:

Yes
No
No

题目大意:给出K个树,判断是否为红黑树;

红黑树是每个节点都带有颜色属性的二叉查找树,颜色为红色或黑色。在二叉查找树强制一般要求以外,对于任何有效的红黑树又有如下的额外要求:(定义来源:红黑树的维基百科

1、节点是红色或黑色。
2、根是黑色。
3、所有叶子都是黑色(叶子是NIL节点)。
4、每个红色节点必须有两个黑色的子节点。(从每个叶子到根的所有路径上不能有两个连续的红色节点。)
5、从任一节点到其每个叶子的所有简单路径都包含相同数目的黑色节点

思路:第4点只需要一次DFS判断就可以解决。第5点的要求,对于每个节点,先获取它的左右子树黑色节点的高度,再进行判断;这里全是递归,真的很绕脑~~

 #include<iostream>
#include<queue>
#include<cmath>
using namespace std;
typedef struct node* BT;
struct node{
int data=;
BT LChild=NULL,RChild=NULL;
}; BT BuildBST(BT t,int num);//建立二叉搜索树
bool NodeColor(BT t);//判断节点的颜色是否符合要求
int getNum(BT t);//获取当前节点的左右子树的高度(仅为黑色节点的高度)
bool isRBT(BT t);//判断是否为红黑树
bool BlackNode(BT t);//判断黑色节点的数量是否符合要求 int main()
{
int K;
scanf("%d",&K);
for(int i=;i<K;i++){
int N;
BT tree=NULL;
scanf("%d",&N);
for(int i=;i<N;i++){
int tmp;
scanf("%d",&tmp);
tree=BuildBST(tree,tmp);
}
if(isRBT(tree))
printf("Yes\n");
else
printf("No\n");
}
return ;
} bool BlackNode(BT t)
{
if(t==NULL) return true;
int Lnum=getNum(t->LChild);
int Rnum=getNum(t->RChild);
if(Lnum!=Rnum) return false;
return BlackNode(t->LChild)&&BlackNode(t->RChild);
} bool isRBT(BT t)
{
return BlackNode(t)&&NodeColor(t)&&t->data>;
} int getNum(BT t)
{
if(t==NULL)
return ;
int Lnum=getNum(t->LChild);
int Rnum=getNum(t->RChild);
return (t->data>)?max(Lnum,Rnum)+:max(Lnum,Rnum);
} bool NodeColor(BT t)
{
if(t==NULL) return true;
if(t->data<){
if((t->LChild!=NULL&&t->LChild->data<)||(t->RChild!=NULL&&t->RChild->data<)){
return false;
}
}
return NodeColor(t->LChild)&&NodeColor(t->RChild);
} BT BuildBST(BT t,int num)
{
if(t==NULL){
t=new node();
t->data=num;
}
else{
if(abs(num)<abs(t->data))
t->LChild=BuildBST(t->LChild,num);
else
t->RChild=BuildBST(t->RChild,num);
}
return t;
}

PAT甲级——1135 Is It A Red-Black Tree (30 分)的更多相关文章

  1. 【PAT甲级】1099 Build A Binary Search Tree (30 分)

    题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...

  2. PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题

    1043 Is It a Binary Search Tree (25 分)   A Binary Search Tree (BST) is recursively defined as a bina ...

  3. 【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)

    题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...

  4. 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)

    题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...

  5. PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra

    题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...

  6. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  7. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  8. PAT 甲级1135. Is It A Red-Black Tree (30)

    链接:1135. Is It A Red-Black Tree (30) 红黑树的性质: (1) Every node is either red or black. (2) The root is ...

  9. pat 甲级 1135. Is It A Red-Black Tree (30)

    1135. Is It A Red-Black Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

随机推荐

  1. Spring 4.2框架中注释驱动的事件监听器详解

    事件交互已经成为很多应用程序不可或缺的一部分,spring框架提供了一个完整的基础设施来处理瞬时事件.下面我们来看看Spring 4.2框架中基于注释驱动的事件监听器. 1.早期的方式 在早期,组件要 ...

  2. Android在有存储卡和无存储卡情况下拍照后固定尺寸和压缩大小

    我最近工作挺忙,距离上一次写博客转眼已经过了一个多月,每次学到和用到点新东西,其实都有分享的欲望,但奈何文笔太差,而一篇文章包括构思,排版,修改发布的时间最少要花费2个小时(这其中还不包括写完后未保存 ...

  3. 03-树3 Tree Traversals Again(25 point(s)) 【Tree】

    03-树3 Tree Traversals Again(25 point(s)) An inorder binary tree traversal can be implemented in a no ...

  4. ubuntu下安装android模拟器genymotion【转】

    本文转载自:http://www.jianshu.com/p/e6062ebb8fc9 去genymotion下载对应的安装包genymotion-2.4.0_x64.bin sudo ./genym ...

  5. SDUT 2402 水杯最小表面积问题

    水杯 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 天气逐渐变热了,准备考研的高童鞋打算在夏天来临之前自己动手造一个水杯,以备口 ...

  6. LightOJ - 1030 Discovering Gold —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1030 1030 - Discovering Gold    PDF (English) Statistics For ...

  7. CSU1553 Good subsequence —— 二分 + RMQ/线段树

    题目链接: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1553 Description Give you a sequence of n n ...

  8. mysql general log开启

    #先查看当前状态 mysql> show variables like 'general%'; +------------------+----------------------------- ...

  9. runtime之方法的交换

    工作中没怎么用到runtime的东西,所以一直没怎么看,现在开始拿起来. runtime之方法的交换: 都知道OC中有category可以对已知类进行扩展,但是假如工程中需要修改某类的原方法,若用ca ...

  10. UUID 和 GUID 的区别(转)

    UUID是一个由4个连字号(-)将32个字节长的字符串分隔后生成的字符串,总共36个字节长.比如:550e8400-e29b-41d4-a716-446655440000 http://gohands ...