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)各种跑不起来,懊恼之后在网上疯狂搜索资 ...
随机推荐
- PHP + Apache 在 Linux(centos7)系统下的环境搭建,基于 yum
(本文采用的是 Centos7 的操作系统,简单起见,以下全部采用 yum 安装,有这么好用的东西为什么要自己去一个一个编译呢) 1, 安装 Apache => yum -y install ...
- 白鹭引擎 - 对象的添加与删除 ( 开关效果 addChild, removeChild )
class Main extends egret.DisplayObjectContainer { /** * Main 类构造器, 初始化的时候自动执行, ( 子类的构造函数必须调用父类的构造函数 ...
- 高级js--(面向对象js,arguments,闭包,自调)
1. Arguments对象 l检测参数个数 1.在函数代码中,使用特殊对象 arguments,开发者无需明确指出参数名,就能访问它们. function howManyArgs() { al ...
- 【Source Insight 】之marco学习笔记1
我们学习编程语言都是从Hello World!,现在我们学习marco也不例外 打开C:\Users\%USERPROFILE%\Documents\Source Insight 4.0\Projec ...
- JEECG-P3开发专题 - 开发环境搭建入门
官方标准开发工具: 1 .IDE Eclipse Java EE IDE for Web Developers. Version: Mars.2 Release (4.5.2) Build id: 2 ...
- ueditor修改工具栏固定位置和显示空白div
ueditor.all.js
- jquery下插入标签以及clone的应用
//内部插入 插入一个儿子 //var $ele = $("<h1></h1>")//创建h1标签 // $ele.html('hello') // $el ...
- Warning:Configuration 'compile' is obsolete and has been replaced with 'implementation'. It will be
1.替换 compile为implementation. 2.file->invalidate caches 或者build中的clear
- Linux 多进程实现方法
1.需求 查找192.168.0.*网段中所有未使用过的IP 2.实现 我们知道查找未使用IP的方法可以使用ping命令完成.对于单个IP的判断,使用命令如下 $ 192.168.0.1 PI ...
- ajaxfileupload.js上传文件兼容IE7及以上版本
要兼容IE789,要修改ajaxfileupload.js;要将此处的代码替换掉 if(window.ActiveXObject) { var io = document.createElement( ...