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)各种跑不起来,懊恼之后在网上疯狂搜索资 ...
随机推荐
- swfupload文件上传配置文件大小
在配置文件中加入: <system.web> <httpRuntime executionTimeout="36000" maxRequestLe ...
- <记录> PHP Redis操作类
namespace common\controller; class Redis { public $redisObj = null; //redis实例化时静态变量 static protected ...
- Linux中的Wheel组的作用
原文:http://www.360doc.com/content/11/0505/10/4644186_114496525.shtml Linux中的Wheel组的作用(用自己的话翻译的) (原文) ...
- java的第一次博客
一枚16年本科毕业的java程序员,至今工作两年,这是我的第一个博客. 谢谢!!!
- Python2 错误记录1File "<string>", line 1, in <module> NameError: name 'f' is not defined
Python 2下 count = 0 while count < 3: user = input('>>>') pwd = input('>>>') if ...
- Nginx ssl证书部署方法
查看当前安装的OpenSSL版本所支持的密码列表,可以使用下列命令:openssl ciphers 苹果ATS检测:https://www.qcloud.com/product/ssl 刚开始&quo ...
- 利用jQuery扩展接口为jQuery框架定义了两个自定义函数,然后调用这两个函数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- week5 03 continus loading news
1.server-side : Rest API 2. client-side 想要持续不断的下拉获取新闻 有两种做法 一种是在UI 我们调用API 获取所有的新闻 然后在UI 拉下的时候显示新闻 其 ...
- linux网卡桥接问题与docker网卡桥接问题
一.linux网卡桥接问题 在linux上创建桥接网卡,与真实的物理网卡进行绑定,相当于在linux中创建了一个虚拟的交换机,以linux网卡地址为源地址的数据,从桥接网卡br0进入,从实际的物理网卡 ...
- 三种方式监听NGUI的事件方法
NGUI研究院之三种方式监听NGUI的事件方法(七) NGUI事件的种类很多,比如点击.双击.拖动.滑动等等,他们处理事件的原理几乎万全一样,本文只用按钮来举例. 1.直接监听事件 把下面脚本直接绑定 ...


