题目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. Python import其他文件夹的文件

    一般情况下,import的文件和被import的文件在同一个路径下面,import也比较方便.如果这两个文件不在一个路径下面,import就比较麻烦了,需要在被import的文件路径下面新建一个__i ...

  2. BitBlt 函数 详解, StretchBlt、SetStretchBltMode、SetBrushOrgEx 按句柄截图、直接截取缩略图

    BitBlt 该函数对指定的源设备环境区域中的像素进行位块(bit_block)转换,以传送到目标设备环境. 函数原型 [DllImport("gdi32.dll")] publi ...

  3. 【原创 深度学习与TensorFlow 动手实践系列 - 2】第二课:传统神经网络

    第二课 传统神经网络 <深度学习>整体结构: 线性回归 -> 神经网络 -> 卷积神经网络(CNN)-> 循环神经网络(RNN)- LSTM 目标分类(人脸识别,物品识别 ...

  4. slfj+logback

    1.pom.xml <dependency> <groupId>org.projectlombok</groupId> <artifactId>lomb ...

  5. Docker安装和使用(转)

    (转)原文地址:https://www.cnblogs.com/xiewenming/p/7903247.html 一,安装Docker 1.1 安装之前保持一个干净的Docker环境 sudo yu ...

  6. win10 caffe python Faster-RCNN训练自己数据集(转)

    一.制作数据集 1. 关于训练的图片 不论你是网上找的图片或者你用别人的数据集,记住一点你的图片不能太小,width和height最好不要小于150.需要是jpeg的图片. 2.制作xml文件 1)L ...

  7. Attacks for RL

    1. http://rll.berkeley.edu/adversarial/   Adversarial Attacks on Neural Network Policies 就是对test时候的p ...

  8. Windows10下virtualenv配置

    1.安装virtualenv pip install virtualenv 2.选定一个目录,作为存储不同环境的总目录 3.安装virtualenvwrapper-powershell(只适用于Pyt ...

  9. IE 浏览器不支持 ES6 Array.from(new Set( )) SCRIPT438: 对象不支持“from”属性

    [转]解决老浏览器不支持ES6的方法 现象: Array.from(new Set( )) SCRIPT438: 对象不支持“from”属性或方法   解决方法: 安装babel 引入browser. ...

  10. 我对CopyOnWrite的思考

    CopyOnWrite 后文中表述为 COW CopyOnWrite容器即写的时候复制一个新的容器进行写:通俗的理解是当我们往一个容器添加元素的时候,不直接往当前容器添加,而是先将当前容器进行Copy ...