PAT1135(红黑书的判定)
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。解这道题的关键就是注意到作为一颗二叉搜索树,左节点的值是小于根节点,右节点的值大于根节点的。
有了这个性质,那么我们就可以根据先序遍历来构造出这棵树。
构造出树以后,根据红黑树的几个条件,我们进行判断就可以了。每个红色节点的左子树和右子树必须是黑色的,任意一个节点到其叶子节点的所有路径上黑色节点的数量是相同的。这两个条件都可以通过递归来实现。
同时,代码中给出了求后序序列的方法
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
struct Node
{
int val;
Node *left,*right;
};
vector<int>arr,pre,post;
Node* build(Node *root ,int v)
{
if(root==NULL)
{
root =new Node();
root->val=v;
root->left=root->right=NULL;
}
else if(abs(v)<=abs(root->val))
root->left=build(root->left,v);
else
root->right=build(root->right,v); return root;
}
void getpost(int root,int end) //获得后序遍历的结果
{
if(root>end)
return;
int i=root+,j=end;
while(i<=end&&pre[root]>pre[i])
i++;
while(j>=root+&&pre[root]<=pre[j])
j--;
if(j+!=i)
return;
getpost(root+,j);//左子树的根和重点
getpost(i,end); //右子树的根和终点
post.push_back(pre[root]);
}
bool judge1(Node *root)
{
if(root==NULL)
return true;
if(root->val<) //红色节点
{
if(root->left!=NULL&&root->left->val<)
return false;
if(root->right!=NULL&&root->right->val<)
return false;
}
return judge1(root->left)&&judge1(root->right); //递归判断
}
int getnum(Node *root)
{
if(root==NULL)
return ;
int l=getnum(root->left);
int r=getnum(root->right);
return root->val> ? max(l,r)+ : max(l,r); //本身就为黑色节点
}
bool judge2(Node *root)
{
if(root==NULL)
return true;
int l=getnum(root->left);
int r=getnum(root->right);
if(l!=r)
return false;
return judge2(root->left)&&judge2(root->right);//递归进行判断
}
int main()
{
int k,n;
scanf("%d",&k);
for(int i=;i<=k;i++)
{
scanf("%d",&n);
arr.resize(n+);
pre.resize(n+);
Node *root=NULL;
for(int j=;j<=n;j++)
{
scanf("%d",&arr[j]);
root=build(root,arr[j]);
pre[j]=abs(arr[j]);
}
post.clear();
getpost(,n);
if(arr[]<||judge1(root)==||judge2(root)==)
printf("No\n");
else
printf("Yes\n");
}
}
PAT1135(红黑书的判定)的更多相关文章
- 刘汝佳黑书 pku等oj题目
原文地址:刘汝佳黑书 pku等oj题目[转]作者:小博博Mr 一.动态规划参考资料:刘汝佳<算法艺术与信息学竞赛><算法导论> 推荐题目:http://acm.pku.edu. ...
- MFC实现红黑砖块
MFC实现红黑砖块 题目 老题目了,给定w,h长宽的图,上面有颜色不同的瓷砖,黑和红,问从给的起点出发,只能走黑色瓷砖,能走多少块,可视化输出过程 思路 咋一看搜索水题,但是要用可视化,要用模板类,, ...
- 【转载】关于在vs2013中配置opengl红宝书第八版环境
本文为转载 原文地址 http://blog.csdn.net/qq821869798/article/details/45247241 本人刚开始学习opengl,买了一本opengl红宝书第八版 ...
- 数据结构--树(遍历,红黑,B树)
平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...
- POI2001 Gold mine(二叉排序树 黑书经典)
采矿(KOP) 金矿的老师傅年底要退休了.经理为了奖赏他的尽职尽责的工作,决定送他一块长方形地.长度为S,宽度为W.老师傅可以自己选择这块地.显然其中包含的采金点越多越好.你的任务就是计算最多能得到多 ...
- 从今日起,我会把OpenGL红宝书上的例子用完整的代码形式写在我的博客中,
1.使用教程:OpenGL红宝书第8版 2.使用的库工具:GLEW和GLFW 3.使用的IDE:vs2012 4.说说目的:完整的看一遍OpenGL,加深印象并且熟练掌握运用OpenGL 5.欢迎有相 ...
- OpenGl编程指南第7版(红宝书)环境配制
环境 OS:win7 旗舰版SP1 64位 编译器: VS 2013 express 的cl 软件 glut. 在这个页面https://www.opengl.org/resources/librar ...
- 蓝绿部署、红黑部署、AB测试、灰度发布、金丝雀发布、滚动发布的概念与区别(转)
出处:https://www.baidu.com/link?url=QjboallwNm_jxcL3fHG57wEakiBfAs_3-TChTGu1eBXstlHEsGBc-NDA7AKTqsiroB ...
- [转] VS2015中跑OpenGL红宝书第八版的第一章示例代码,运行
Ori Article Link OpenGL的东西快忘光了,把角落的第八版红宝书拿出来复习一下 从书中的地址下了个示例代码结果新系统(Win10+VS2015)各种跑不起来,懊恼之后在网上疯狂搜索资 ...
随机推荐
- visio交叉线不凸起
使用visio作图时,经常会遇到交叉线在相交时会形成一个弯曲弓形,这有时十分影响视图效果.可以采用下面的方法消除弓形. 1.visio2003:只需要选中该交叉线,选择“格式”->“行为”,在打 ...
- Spring boot http, https
1:同时支持http, https配置 参考文献: https://www.cnblogs.com/lianggp/p/8136540.html 2:http 转发到 https配置 @Bean p ...
- linux下安装jdk,tomcat,maven
1. jdk安装 下载jdk的linux版本. >tar -zxvf jdk1.8.0_191.tar.gz 配置环境变量: >vim /etc/profile最前面添加: expor ...
- 伪AJAX
<h3>3,伪ajax</h3> <h6>学习iframe(嵌套别人家网站的)</h6> <div> <input id=" ...
- windows下如何查看端口,关闭端口,开启端口
如何查看端口 在Windows 2000/XP/Server 2003中要查看端口,可以使用NETSTAT命令: “开始">"运行”>“cmd”,打开命令提示符窗口.在 ...
- Android内存优化相关
Android的内存管理方式 Android系统内存分配与回收方式 一个APP通常就是一个进程对应一个虚拟机 GC只在Heap剩余空间不够时才去垃圾回收 GC触发时,所有线程都会被暂停!!! APP内 ...
- img标签插入图片返回403,浏览器可以直接打开
参考:https://segmentfault.com/q/1010000011752614/a-1020000011764026 博客园引入外部图片出现,出现403问题,应该是加了防盗链,会检测访问 ...
- Spring格式化注解
Spring Framework 3.0发布了.这里我们介绍其中的一个:用于格式化的注解.简介 Spring 3 提供了两个可以用于格式化数字.日期和时间的注解@NumberFormat和@DateT ...
- ADO.Net创建数据模型和数据访问类及泛型集合
数据模型和数据访问类:数据模型: 使用面向对象中的封装特性,将数据表中的行数据组成一个同样结构的对象,来单独使用: 数据访问类: 将某一个表的全部增删改查操作的方法写进去,方便统一管理和调用: 数据模 ...
- Numpy知识(二)
ndarray的简单数学计算就和普通的a+b,a-b,a*b,a/b等类似. 关于ndarray的切片: arr[n]:寻找第n个元素(针对一维)arr[n:m]:从下标为n元素开始,截取到下标为m- ...