PAT甲级1135 Is It A Red-Black Tree?【dfs】
题目: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】的更多相关文章
- PAT甲级1123. Is It a Complete AVL Tree
PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...
- 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 ...
- 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 ...
- PAT甲级——1135 Is It A Red-Black Tree (30 分)
我先在CSDN上面发表了同样的文章,见https://blog.csdn.net/weixin_44385565/article/details/88863693 排版比博客园要好一些.. 1135 ...
- PAT 甲级 1135 Is It A Red-Black Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 There is a kind of bal ...
- 【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 ...
- PAT甲级1123 Is It a Complete AVL Tree【AVL树】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...
- PAT 甲级 1043 Is It a Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...
- 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 ...
随机推荐
- 使用Træfɪk(traefik)来加速Qt在线更新
简述 在使用Qt的MaintenanceTool程序进行在线更新的时候遇到一个问题,就是访问download.qt.io实在太慢了,老是失败.所以想使用国内的镜像站来进行更新. 使用Qt的镜像站方法也 ...
- DES算法原理完整版
1.所需参数 key:8个字节共64位的工作密钥 data:8个字节共64位的需要被加密或被解密的数据 mode:DES工作方式,加密或者解密 2.初始置换 DES算法使用64位的密钥key将64位的 ...
- 【C++】C++中的引用与指针
想必大家对C++中的指针都有所了解,但是什么是引用呢?C++11标准引入了“引用”的新功能. 引用 引用(reference):给对象起了另外一个名字,引用类型引用(refers to)另外一种类型, ...
- JAVA方法中的参数用final来修饰的原因
JAVA方法中的参数用final来修饰的原因 很多人都说在JAVA中用final来修饰方法参数的原因是防止方法参数在调用时被篡改,其实也就是这个原因,但理解起来可能会有歧义,有的人认为是调用语句的 ...
- 从零开始unity特效(持续追加中)
打算重拾3d渲染了,计划把主要理论过一遍,每部分琢磨一个言之有物的demo. 因为很多东西要现学,再加上上班-8h,更新会比较慢,但会坚持. (待续) -------houdini+unity河流(2 ...
- 深入 AngularUI Router
翻译:深入 AngularUI Router 原文地址:http://www.ng-newsletter.com/posts/angular-ui-router.html ui-router: htt ...
- vue的js文件中获取vue实例
1.main.js导出vue实例: var vue = new Vue({ el: '#app', router, components: { App }, template: '<App/&g ...
- maven私服不能重复部署解决
1.报错 Return code is: 400, ReasonPhrase: Repository does not allow updating assets: maven-releases. 2 ...
- 通过jQuery实时监听表格行数变化
[本文出自天外归云的博客园] 使用bootstrap table组件,当使用过滤器的时候,页面的表格行数发生变化,此时需要统计表格行数.想要监听表格变化,如何做呢? 使用场景:有一个表格里放着许多测试 ...
- 【gulp】gulp + browsersync 构建前端项目自动化工作流
什么是 gulp? gulp.js 是一个自动化构建工具,开发者可以使用它在项目开发过程中自动执行常见任务.gulp.js 是基于 node.js 构建的,利用 node.js 流的威力,你可以快速构 ...