PAT甲级1135 Is It A Red-Black Tree?【dfs】

时间:2022-03-12 23:58:37

题目https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640

题意:

给定一棵二叉搜索树的先序遍历结果,问这棵树是不是一棵红黑树。

思路:

首先需要明确二叉搜索树和红黑树的性质。

二叉搜索树的每个节点,左子树上的值都比这个节点的值小,右子树上的值都比这个节点的值大。

因此对于一棵二叉搜索树,如果给定了先序遍历结果,就可以唯一确定这棵树了。

红黑树的性质:

1、每个节点是红色或是黑色

2、根节点是黑色的

3、红色节点的儿子一定是黑色的

4、从任意节点到NULL指针的每一条路径的黑色节点数都是相同的

对于这道题,首先我们可以唯一的建树

然后在这棵唯一的树上进行dfs,判断当前节点的左右子树是否是一棵红黑树。边界条件显然是当节点只有一个或没有儿子的时候,此时直接判断。

【啊已经四月了!来不及了!】

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int k, n;
const int maxn = ;
struct node{
int val;
bool isred;
int lchild, rchild;
}nodes[maxn]; int tot = ;
void addnode(int val)
{
if(val < ){
nodes[tot].isred = true;
val = -val;
}
else{
nodes[tot].isred = false;
}
nodes[tot].val = val;
nodes[tot].lchild = nodes[tot].rchild = -; int now = , prev = ;
while(now != -){
prev = now;
if(val > nodes[now].val){
now = nodes[now].rchild;
}
else{
now = nodes[now].lchild;
}
}
if(val > nodes[prev].val){
nodes[prev].rchild = tot++;
}
else{
nodes[prev].lchild = tot++;
}
} bool flag = true;
int dfs(int rt)
{
int l = nodes[rt].lchild, r = nodes[rt].rchild;
int lres = , rres = ;
if(l != -){
if(dfs(l) == -)return -;
lres += dfs(l);
}
if(r != -){
if(dfs(r) == -)return -;
rres += dfs(r);
}
if(l == - && r == -){
return !nodes[rt].isred;
}
if(lres != rres){
return -;
}
if(nodes[rt].isred && (nodes[l].isred || nodes[r].isred)){
return -;
}
return lres + !nodes[rt].isred;
} void printTree(int rt)
{
if(rt == -)return;
printf("%d ", nodes[rt].val);
printTree(nodes[rt].lchild);
printTree(nodes[rt].rchild);
} int main()
{
scanf("%d", &k);
while(k--){
for(int i = ; i <= tot; i++){
nodes[tot].val = ;
nodes[tot].isred = ;
nodes[tot].lchild = nodes[tot].rchild = -;
}
tot = ;
flag = true;
scanf("%d", &n);
scanf("%d", &nodes[tot].val);
nodes[tot].isred = false;
nodes[tot].lchild = nodes[tot].rchild = -;tot++;
for(int i = ; i < n; i++){
int val;
scanf("%d", &val);
addnode(val);
} //printTree(0);
if(nodes[].val < ){
printf("No\n");
}
else{
if(dfs() != -){
printf("Yes\n");
}
else{
printf("No\n");
}
}
}
return ;
}

PAT甲级1135 Is It A Red-Black Tree?【dfs】的更多相关文章

  1. PAT甲级1123&period; Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  2. PAT 甲级1135&period; Is It A Red-Black Tree &lpar;30&rpar;

    链接:1135. Is It A Red-Black Tree (30) 红黑树的性质: (1) Every node is either red or black. (2) The root is ...

  3. pat 甲级 1135&period; Is It A Red-Black Tree &lpar;30&rpar;

    1135. Is It A Red-Black Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  4. PAT甲级——1135 Is It A Red-Black Tree (30 分&rpar;

    我先在CSDN上面发表了同样的文章,见https://blog.csdn.net/weixin_44385565/article/details/88863693 排版比博客园要好一些.. 1135 ...

  5. PAT 甲级 1135 Is It A Red-Black Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 There is a kind of bal ...

  6. 【PAT 甲级】1151 LCA in a Binary Tree (30 分)

    题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has bo ...

  7. PAT甲级1123 Is It a Complete AVL Tree【AVL树】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...

  8. PAT 甲级 1043 Is It a Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...

  9. PAT甲级——1123 Is It a Complete AVL Tree (完全AVL树的判断)

    嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...

随机推荐

  1. html5 audio总结

    前言 html5中对音频,视频播放原生支持.最近做了一个音乐播放器,得益于快过年了,才能抽出一点时间来总结一下.总的来说,html5对audio的支持非常强大, 难怪flash要死.浏览器上装播放插件 ...

  2. 转:TopN推荐系统——推荐的实现与推荐效果的评价指标

    转自:用户推荐系统_python 代码-豆瓣书籍:项亮的<推荐系统实践> import random import math class UserBasedCF: def __init__ ...

  3. Python安装后在CMD命令行下出现&OpenCurlyDoubleQuote;应用程序无法启动&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;&period;”问题

    问题存在之一:系统是刚刚重做的精简版服务器系统(阉割版) AN就是在阿里云上刚开的Windows Server 2008 系统上碰到的  吓尿了都 症状:            正常安装python环 ...

  4. UML--核心元素之业务实体

    如果说参与者和用例描述了我们在这个问题领域中达到什么样的目标,那么业务实体就描述了我们使用什么来达到业务目标以及通过什么来记录这个业务目标. 如果把问题领域比喻成一幢大楼的话,业务实体就是构成这幢大楼 ...

  5. The Swift Programming Language-官方教程精译Swift(9) 枚举-- --Enumerations

    枚举定义了一个通用类型的一组相关的值,使你可以在你的代码中以一个安全的方式来使用这些值.   如果你熟悉 C 语言,你就会知道,在 C 语言中枚举指定相关名称为一组整型值.Swift 中的枚举更加灵活 ...

  6. SqlBulkCopy实现大容量数据快速插入数据库中

    一般情况下,我们手写sqlhelper类,在此类中定义一个数据插入到数据库的一个方法.将数据库连接密封在using()的语句中.using显示了Idispose接口.可以及时释放数据库连接资源.代码如 ...

  7. &lbrack;UVAlive4297&rsqb;First Knight

    题面在这里 题意 给定一个\(n\times m\)的格网,从\((1,1)\)出发,每一格\((i,j)\)往上下左右移动的概率已经给出,询问到达\((n,m)\)的期望步数 数据范围 \[n,m\ ...

  8. java面试基础题------》Java 中List、Set、Map异同点

    借鉴地址:http://blog.csdn.net/speedme/article/details/22398395 几句喜欢的话,拷贝下来: 世间上本来没有集合,(只有数组参考C语言)但有人想要,所 ...

  9. 由n个元素组成的数组,n-2个数出现了偶数次,两个数出现了奇数次,且这两个数不相等,如何用O&lpar;1&rpar;的空间复杂度,找出这两个数

    思路分析: 方法一:涉及到两个数,就要用到异或定理了:若a^b=x,则a=b^x,b=x^a.对于这道题,假设这两个数分别为a.b,将数组中所有元素异或之后结果为x,因为a!=b,所以x=a^b,且x ...

  10. 170823、SQL Update多表联合更新的方法

    SQL Update多表联合更新的方法 (1) sqlite 多表更新方法 update t1 set col1=t2.col1 from table1 t1 inner join table2 t2 ...