PAT Advanced 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:
39
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
题目分析
已知二叉查找树前序序列,判断其是否为红黑树(红节点用负数表示,黑节点用整数表示)
解题思路
- 前序序列建树(根节点大于等于左子树所有节点,小于右子树所有节点)或者依次插入建树
- 判断是否为红黑树
2.1 判断根节点是否为黑色
2.2 判断每个节点到其子树叶子节点的黑色结点数相同
2.3 判断红色节点的左右子节点都为黑色
Code
Code 01
#include <iostream>
#include <vector>
using namespace std;
struct node {
int v;
node * f;
node * r;
node (){}
node (int _v):v(_v){
f=r=NULL;
}
};
vector<int> pre;
// 前序建树
node * create(int preL,int preR){
if(preL>preR)return NULL;
node * root = new node(pre[preL]);
int k=preL+1;
while(k<preR&&abs(pre[k])<=abs(pre[preL]))k++;//找第一个大于根节点的值
root->f=create(preL+1,k-1);
root->r=create(k,preR);
return root;
}
// 判断红色节点的两个孩子是否都是黑色节点
bool judge1(node * root) {
if(root==NULL)return true;
if(root->v<0) {
if(root->f!=NULL&&root->f->v<0)return false;
if(root->r!=NULL&&root->r->v<0)return false;
}
return judge1(root->f)&&judge1(root->r);
}
// 获取当前节点高度(高度指:从当前节点到其子树的叶子节点的黑色结点数)
int getNum(node * root) {
if(root==NULL)return 1;
int f = getNum(root->f);
int r = getNum(root->r);
return root->v>0?max(f,r)+1:max(f,r);
}
// 判断每个节点到其子树的叶子节点的黑色节点数相同
bool judge2(node * root) {
if(root==NULL) return true;
int f= getNum(root->f);
int r= getNum(root->r);
if(f!=r)return false;
return judge2(root->f)&&judge2(root->r);
}
int main(int argc,char * argv[]) {
int k,n;
scanf("%d",&k);
for(int i=0; i<k; i++) {
scanf("%d",&n);
pre.clear();
pre.resize(n);
for(int j=0; j<n; j++) {
scanf("%d",&pre[j]);
}
node * root=create(0,n-1);
if(root->v<0||judge1(root)==false||judge2(root)==false) {
printf("No\n");
}else printf("Yes\n");
}
return 0;
}
Code 02
#include <iostream>
using namespace std;
struct node {
int v;
node * f;
node * r;
node (){}
node (int _v):v(_v){
f=r=NULL;
}
};
// 前序建树
void insert(node * &root,int v) {
if(root==NULL) {
root=new node(v);
return;
}
if(abs(v)<=abs(root->v))
insert(root->f, v);
else
insert(root->r,v);
}
// 判断红色节点的两个孩子是否都是黑色节点
bool judge1(node * root) {
if(root==NULL)return true;
if(root->v<0) {
if(root->f!=NULL&&root->f->v<0)return false;
if(root->r!=NULL&&root->r->v<0)return false;
}
return judge1(root->f)&&judge1(root->r);
}
// 获取当前节点高度(高度指:从当前节点到其子树的叶子节点的黑色结点数)
int getNum(node * root) {
if(root==NULL)return 1;
int f = getNum(root->f);
int r = getNum(root->r);
return root->v>0?max(f,r)+1:max(f,r);
}
// 判断每个节点到其子树的叶子节点的黑色节点数相同
bool judge2(node * root) {
if(root==NULL) return true;
int f= getNum(root->f);
int r= getNum(root->r);
if(f!=r)return false;
return judge2(root->f)&&judge2(root->r);
}
int main(int argc,char * argv[]) {
int k,n;
scanf("%d",&k);
for(int i=0; i<k; i++) {
scanf("%d",&n);
int nds[n];
node * root=NULL;
for(int j=0; j<n; j++) {
scanf("%d",&nds[j]);
insert(root,nds[j]);
}
if(root->v<0||judge1(root)==false||judge2(root)==false) {
printf("No\n");
}else printf("Yes\n");
}
return 0;
}
PAT Advanced 1135 Is It A Red-Black Tree (30) [红⿊树]的更多相关文章
- PAT Advanced 1123 Is It a Complete AVL Tree (30) [AVL树]
题目 An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child ...
- PAT (Advanced Level) 1099. Build A Binary Search Tree (30)
预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30)
简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- PAT (Advanced Level) 1087. All Roads Lead to Rome (30)
暴力DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT Advanced 1043 Is It a Binary Search Tree (25) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- PAT Advanced 1094 The Largest Generation (25) [BFS,DFS,树的遍历]
题目 A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level ...
- Red Black Tree(红黑树)
(修改于 2018-05-06 15:53:22 还差删除维护操作.层序遍历没完成.维护操作没完成不想写层序遍历怎么办...) 今天下午完成了红黑树的插入的维护操作,但删除的维护操作还没有解决,删除的 ...
- PAT甲级1123 Is It a Complete AVL Tree【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...
- PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- NO14 快照-克隆-必须掌握的Linux目录结构
壹 VMware克隆,快照讲解及相应问题讲解: ·快照:比喻:假设把人生作一个快照.1岁10岁20岁6无限还原到前一个设置的节点. ·克隆学习一般用链接克隆,不另外占用磁盘,但是依赖本体虚拟机.完整 ...
- 实训23 功能FC的建立与调用
第4章:实训23 功能的生成与条用 功能简称FC 是用户编写的没有自己存储区的逻辑块 . 功能主要用来执行条用一次就可以完成的操作. 类似于C语言中的 函数 步骤一 单击确定 以后 出现了 在下面图框 ...
- 关于VMware vSphere Client安装时,.net framework4进度条卡住不动(亲测)
亲测有用的办法 1.点击电脑桌面右下角的"开始"按钮,点击"运行"按钮,在弹出的节目输入框中输入"regedit". 2.在弹出来的&quo ...
- P1042 字符统计
P1042 字符统计 转跳点:
- 154. 寻找旋转排序数组中的最小值 II
转跳点:--\(˙<>˙)/-- 原本打算大年三十十一起写完的,结果这篇拖到了年初一…… 这道题比刚刚那道,麻烦一点,因为有重复,所以我们需要考虑重复的情况,就是刚刚的两种情况变成了三种: ...
- HDU - 1698 Just a Hook (线段树---区间修改)
题意:n个棍子,初始值全为1,给定Q个区间,分别赋值,问n个棍子的总值. 分析:lazy标记主要体现在update上. 当l <= L && R <= r时,该结点的子结点 ...
- 阿里云香港服务器IIS发布网站不成功解决方法
刚刚弄好了一个阿里云上服务器,费老劲儿了.我买了一个香港的服务器,最低配置,专有网络,买着玩的,一个.win的域名,省的国内备案了. 遇到的问题是怎么也访问不了我IIS上发布的网站,我把我解决方法说下 ...
- eclipse启动tomcat访问localhost:8080报404
直接双击tomcat\bin目录下面的startup.bat启动 是没问题 的 但是eclipse启动tomcat访问localhost:8080报404 解决方案如下: 双击红色圈里面的tomcat ...
- java简写名词解释
RPC(Remote Procedure Call)—远程过程调用 实时编译器(Just In Time Compiler,JIT) XML 指可扩展标记语言(EXtensible Markup La ...
- 转载-Logistic回归总结
Logistic回归总结 作者:洞庭之子 微博:洞庭之子-Bing (2013年11月) 1.引言 看了Stanford的Andrew Ng老师的机器学习公开课中关于Logistic Regress ...