PAT甲级——1135 Is It A Red-Black Tree (30 分)
我先在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 分)的更多相关文章
- 【PAT甲级】1099 Build A Binary Search Tree (30 分)
题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...
- 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 ...
- 【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)
题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...
- 【PAT甲级】1087 All Roads Lead to Rome (30 分)(dijkstra+dfs或dijkstra+记录路径)
题意: 输入两个正整数N和K(2<=N<=200),代表城市的数量和道路的数量.接着输入起点城市的名称(所有城市的名字均用三个大写字母表示),接着输入N-1行每行包括一个城市的名字和到达该 ...
- PAT甲级练习 1087 All Roads Lead to Rome (30分) 字符串hash + dijkstra
题目分析: 这题我在写的时候在PTA提交能过但是在牛客网就WA了一个点,先写一下思路留个坑 这题的简单来说就是需要找一条最短路->最开心->点最少(平均幸福指数自然就高了),由于本题给出的 ...
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 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 ...
- 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 ...
随机推荐
- Could not find com.android.tools.lint:lint-gradle:26.1.2.
allprojects { repositories { flatDir { dirs 'libs' } jcenter() google() }}
- 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】
02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...
- spring boot 使用redis 及redis工具类
1-添加maven依赖 2-添加redis配置 3-工具类 1-添加maven依赖 实际上是封装了jedis <!-- redis 依赖--> <dependency> < ...
- 基于springboot的Dubbo的常规总结
1.引入jar包: <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>com.alibaba.spring.b ...
- ThinkPHP Widget模块开发流程
初识ThinkPHP的Widget,现把模块开发的流程发布如下,也方便以后自己查阅: 一.新建数据库表self_modules,sql代码如下 CREATE TABLE `self_modules` ...
- wav音频文件头解析
wav概述 WAV为微软公司(Microsoft)开发的一种声音文件格式,它符合RIFF(ResourceInterchange File Format)文件规范,用于保存Windows平台的音频信息 ...
- hihocoder #1103 : Colorful Lecture Note微软苏州校招笔试 1月10日(字符串处理+栈)
#1103 : Colorful Lecture Note 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is writing an algorit ...
- sqlserver锁表查看
sp_lock--查询哪个进程锁表了,spid:进程ID,ObjId:对象ID EXEC sp_executesql N'KILL [spid]'--杀进程 select object_name([O ...
- 通用方法解决UITextFiled输入的时候,键盘遮挡问题
我们在用键盘录入的时候,有可能会遮挡录入框,所以我们应调整UIView的位置,使其不被遮挡.我写了一个通用的方法可以解决这个问题:1. [代码][C/C++]代码 - (void) ...
- Peach+Fuzzer
目录 1 Peach是什么....................................................................................... ...