题目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. Is It a Complete AVL Tree

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

  2. PAT 甲级1135. Is It A Red-Black Tree (30)

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

  3. pat 甲级 1135. Is It A Red-Black Tree (30)

    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 分)

    我先在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. java写桌面程序

    一:使用java swing开发窗口程序 简述: 1.文章内容主要是使用java swing类库开发一个小的窗口程序,然后使用exe4j发布成exe可以安装的程序,让初学者对使用java来做pc软件开 ...

  2. 单表40G,不要吃惊,还能往里面插入数据,哈哈

    单表40G,不要吃惊,还能往里面插入数据,哈哈 上图:

  3. python和C++联合调试

    python可以利用SO的方式去调用C++中的函数,但是需要一种调试方案来进行python和C++的联合调试,效果是直接在c++代码中打断点,然后python在进行c++so调用的时候,直接进入到断点 ...

  4. 安全工具-Sparta

    Sparta是一个集端口扫描.网络扫描.服务探测以及暴力破解等多项功能于一身的工具,kali中已经预装了该工具,可直接使用. > 输入目标IP,开始扫描即可探测出开放的端口及服务 > 选中 ...

  5. fiddler抓包url有乱码

    fiddler抓包url有乱码: 解决具体步骤: 注册表:regedit   HKEY_CURRENT_USER\Software\Microsoft\Fiddler2 1.打开注册表,regedit ...

  6. SpringBoot集成RabbitMQ消息队列搭建与ACK消息确认入门

    1.RabbitMQ介绍 RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.Rabbi ...

  7. 具体解释MVP矩阵之ViewMatrix

    矩阵推导 ViewMatrix用于直接将World坐标系下的坐标转换到Camera坐标系下.已知相机的坐标系.还有相机在世界空间下的坐标.就能够求出ViewMatrix.以下进行具体推导. 令UVN为 ...

  8. yml 后面的配置覆盖前面的

    事情是这样的: a: b: c: tomcat d: hoho # 中间还隔了好多 a: b: c: tomcat 这种情况下,后面的配置会覆盖前面的所有配置,即 a.d = hoho 也会被覆盖 y ...

  9. [转]理解Go语言中的nil

    最近在油管上面看了一个视频:Understanding nil,挺有意思,这篇文章就对视频做一个归纳总结,代码示例都是来自于视频. nil是什么 相信写过Golang的程序员对下面一段代码是非常非常熟 ...

  10. java生成兑换码礼包码--工具类

    import java.util.HashSet; import java.util.Random; import java.util.Set; public class GenSerial { pr ...