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 ...
随机推荐
- SpringBoot-(8)-配置MySQL数据库链接,配置数据坚挺拦截,创建默认数据表
一,链接mysql数据库 # 数据源基本配置 spring.datasource.username=root spring.datasource.password=123456 spring.data ...
- requirejs测试
参考资料:http://www.ruanyifeng.com/blog/2012/11/require_js.html 一.文件目录 二.html <!DOCTYPE html> < ...
- bzoj5259: [Cerc2017]区间
还是很强的一个题 ORZ肉丝哥哥 对于两个相交区间,假如他们两个都是可行的,那么他们的交也可行,不然没可能两边都把它缺的补上 那么对于答案区间,向右找到第一个可行区间右端点覆盖询问区间,就是最优的 考 ...
- RobotFramework教程使用笔记——初识RobotFramework
1.创建项目 File->New Project 创建测试项目 Type选择Directory 右键项目创建测试套件,也可以理解为创建不同测试逻辑或者是测试业务 右键测试套件创建case 注:如 ...
- Android图片加载神器之Fresco-加载图片基础[详细图解Fresco的使用](秒杀imageloader)
Fresco简单的使用—SimpleDraweeView 百学须先立志—学前须知: 在我们平时加载图片(不管是下载还是加载本地图片…..)的时候,我们经常会遇到这样一个需求,那就是当图片正在加载时应该 ...
- cassandra 存储list数组
demo如下: CREATE TABLE users3 ( user_id text PRIMARY KEY, first_name text, last_name text, emails list ...
- Xamarin.Forms初始
前言 Xamarin.Forms 为 .NET 开发人员提供一个完整的跨平台 UI 工具包. 在 Visual Studio 中使用 C# 生成完全本机的 Android.iOS 和通用 Window ...
- caffe源码学习
本文转载自:https://buptldy.github.io/2016/10/09/2016-10-09-Caffe_Code/ Caffe简介 Caffe作为一个优秀的深度学习框架网上已经有很多内 ...
- ASoC框架
ASoC框架分为3部分: 1. platform(用来描述芯片的DAI接口,负责数据传输): DAI:snd_soc_dai_driver, 用来表示支持哪些格式数据, 提供设置格式的函数, 启动数据 ...
- C#中,当鼠标移动到控件上,动态显示提示内容 ToolTip
[背景]写了个C#的小程序,其中在Settings窗体中有些配置参数,希望在鼠标移动到对应的配置选项上,动态显示相关的说明解释内容. [解决过程]1.原先就已经知道,有些控件,是带ToolTipTex ...