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(红黑书的判定)的更多相关文章

  1. 刘汝佳黑书 pku等oj题目

    原文地址:刘汝佳黑书 pku等oj题目[转]作者:小博博Mr 一.动态规划参考资料:刘汝佳<算法艺术与信息学竞赛><算法导论> 推荐题目:http://acm.pku.edu. ...

  2. MFC实现红黑砖块

    MFC实现红黑砖块 题目 老题目了,给定w,h长宽的图,上面有颜色不同的瓷砖,黑和红,问从给的起点出发,只能走黑色瓷砖,能走多少块,可视化输出过程 思路 咋一看搜索水题,但是要用可视化,要用模板类,, ...

  3. 【转载】关于在vs2013中配置opengl红宝书第八版环境

     本文为转载 原文地址 http://blog.csdn.net/qq821869798/article/details/45247241 本人刚开始学习opengl,买了一本opengl红宝书第八版 ...

  4. 数据结构--树(遍历,红黑,B树)

    平时接触树还比较少,写一篇博文来积累一下树的相关知识. 很早之前在数据结构里面学的树的遍历. 前序遍历:根节点->左子树->右子树 中序遍历:左子树->根节点->右子树 后序遍 ...

  5. POI2001 Gold mine(二叉排序树 黑书经典)

    采矿(KOP) 金矿的老师傅年底要退休了.经理为了奖赏他的尽职尽责的工作,决定送他一块长方形地.长度为S,宽度为W.老师傅可以自己选择这块地.显然其中包含的采金点越多越好.你的任务就是计算最多能得到多 ...

  6. 从今日起,我会把OpenGL红宝书上的例子用完整的代码形式写在我的博客中,

    1.使用教程:OpenGL红宝书第8版 2.使用的库工具:GLEW和GLFW 3.使用的IDE:vs2012 4.说说目的:完整的看一遍OpenGL,加深印象并且熟练掌握运用OpenGL 5.欢迎有相 ...

  7. OpenGl编程指南第7版(红宝书)环境配制

    环境 OS:win7 旗舰版SP1 64位 编译器: VS 2013 express 的cl 软件 glut. 在这个页面https://www.opengl.org/resources/librar ...

  8. 蓝绿部署、红黑部署、AB测试、灰度发布、金丝雀发布、滚动发布的概念与区别(转)

    出处:https://www.baidu.com/link?url=QjboallwNm_jxcL3fHG57wEakiBfAs_3-TChTGu1eBXstlHEsGBc-NDA7AKTqsiroB ...

  9. [转] VS2015中跑OpenGL红宝书第八版的第一章示例代码,运行

    Ori Article Link OpenGL的东西快忘光了,把角落的第八版红宝书拿出来复习一下 从书中的地址下了个示例代码结果新系统(Win10+VS2015)各种跑不起来,懊恼之后在网上疯狂搜索资 ...

随机推荐

  1. VMware设置cpu虚拟化,intel VT-x

    1.关闭虚拟机 2.右键需要更改的虚拟机--设置--处理器

  2. 01.hadoop集群环境搭建

    hadoop集群搭建的步骤 1.安装jdk2修改ip地址3.关闭防火墙4.修改hostname5.设置ssh自动登陆6.安装hadoop-------------------------------- ...

  3. leetcode509

    public class Solution { public int Fib(int N) { ) { ; } ) { ; } else { List<int> list = new Li ...

  4. js 验证码倒计时

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. AIR程序调用本地默认应用程序打开本地文件

    当我用下面语句的时候,可以成功打开桌面文件夹下面的文件: var file:File = File.desktopDirectory.resolvePath("test.jpg") ...

  6. ubuntu交换Caps 和 ESC

    https://askubuntu.com/questions/363346/how-to-permanently-switch-caps-lock-and-esc This will allow y ...

  7. PS常用快捷键(收藏)

    一.工具箱(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取) 矩形.椭圆选框工具 [M] 移动工具 [V] 套索.多边形套索.磁性套索 [L] 魔棒工具 [W] 裁剪工具 [C] 切片工 ...

  8. (转)关闭win10的Skype

    https://blog.csdn.net/qq_38285661/article/details/86663849 使用win10的小伙伴们,有没有发现一个不用的功能Skype,假如你想卸载又怕卸不 ...

  9. Uni2D 入门 -- Skeletal Animation

    转载 csdn http://blog.csdn.net/kakashi8841/article/details/17615195 Skeletal Animation Uni2D V2.0 引进了一 ...

  10. 学Android开发的人可以去的几个网站

    学Android开发的人可以去的几个网站 1.<IT蓝豹>Android开源项目分享平台国内非常好的一个Android开发者分享站,分享android所有特效,每天都有最新的Android ...