PAT A1135 Is It A Red Black Tree
判断一棵树是否是红黑树,按题给条件建树,dfs判断即可~
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
int data;
node * left=NULL;
node * right=NULL;
};
void insert (node * &root,int x) {
if (root==NULL) {
root=new node;
root->data=x;
return;
}
if (abs(x)<abs(root->data)) insert (root->left,x);
else insert (root->right,x);
}
int getnum (node * root) {
if (root==NULL) return ;
if (root->data>)
return max(getnum(root->left),getnum(root->right))+;
else return max(getnum(root->left),getnum(root->right));
}
int flag=;
void dfs (node * root) {
if (root==NULL) return;
if (root->data<&&root->left&&root->left->data<) flag++;
if (root->data<&&root->right&&root->right->data<) flag++;
if (getnum(root->left)!=getnum(root->right)) flag++;
dfs (root->left);
dfs (root->right);
}
int main () {
int T;
scanf ("%d",&T);
int N,x;
while (T--) {
node * root=NULL;
scanf ("%d",&N);
for (int i=;i<N;i++) {
scanf ("%d",&x);
insert (root,x);
}
flag=;
if (root->data<) flag++;
dfs (root);
if (flag==) printf ("Yes\n");
else printf ("No\n");
}
return ;
}
PAT A1135 Is It A Red Black Tree的更多相关文章
- [转载] 红黑树(Red Black Tree)- 对于 JDK TreeMap的实现
转载自http://blog.csdn.net/yangjun2/article/details/6542321 介绍另一种平衡二叉树:红黑树(Red Black Tree),红黑树由Rudolf B ...
- Red–black tree ---reference wiki
source address:http://en.wikipedia.org/wiki/Red%E2%80%93black_tree A red–black tree is a type of sel ...
- Red Black Tree 红黑树 AVL trees 2-3 trees 2-3-4 trees B-trees Red-black trees Balanced search tree 平衡搜索树
小结: 1.红黑树:典型的用途是实现关联数组 2.旋转 当我们在对红黑树进行插入和删除等操作时,对树做了修改,那么可能会违背红黑树的性质.为了保持红黑树的性质,我们可以通过对树进行旋转,即修改树中某些 ...
- PAT甲级:1066 Root of AVL Tree (25分)
PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- CF1208H Red Blue Tree
CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有 ...
- 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 ...
- 2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)
BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. Among ...
- PAT 甲级 1135 Is It A Red-Black Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 There is a kind of bal ...
随机推荐
- wampserver apache 403无权限访问 You don't have permission to access /index.html on this server
今天接到要求 需要配置一下https 折腾好久 最后好还遇到了权限不够的问题 最后解决方案如下 我这边补充一下我的方法 我的apache是 2.4.23 版本 是由 wampserver集成的 在 h ...
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- nginx autoindex 配置目录浏览功能
Nginx打开目录浏览功能 yum install httpd-tools -y cd /usr/local/openrestry/nginx/conf/ htpasswd -c passwd adm ...
- 每天进步一点点------SOPC TIMER (一)
寄存器图 可以通过操作以下的寄存器来实现对timer(定时器)内核的操作(仅描述32位计数器) 状态寄存器: TO(timeout) :计数器计数到0时,该位置1,之后TO位的值会保持,直到手动清零, ...
- CSH while read
- +(new Date())
+(new Date()) 等于 new Date().getTime();展示 1561003191879 毫秒时间戳
- markdown整理
html标签# h1 ## h2 ### h3 #### h4 ##### h5 ###### h6 一级标题:内容=== 二级标题:内容--- 强调文字:>内容 链接:[文字](链接地址) 图 ...
- django 模版查找路径
路径的配置,模版上下文的配置等.模版路径可以在两个地方配置. 1.'DIRS':这是一个列表,在这个列表中可以存放所有的模版路径,以后在视图中使用render或者render_to_string渲染模 ...
- awk命令_Linux awk 命令用法详解
本文索引 awk命令格式和选项 awk模式和操作 模式 操作 awk脚本基本结构 awk的工作原理 awk内置变量(预定义变量) 将外部变量值传递给awk awk运算与判断 算术运算符 赋值运算符 逻 ...
- nginx 解决 connect() failed (111: Connection refused) while connecting to upstream,
嗯哼,刚装了个ubuntu的lnmp,我的天啊,踩的坑比我脂肪还多了 比如刚装完的时候访问显示502, 也不知道什么问题,就去看了一下nginx日志 /var/log/nginx/error.log ...