pat 甲级 1135. Is It A Red-Black Tree (30)
1135. Is It A Red-Black Tree (30)
There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties:
(1) Every node is either red or black.
(2) The root is black.
(3) Every leaf (NULL) is black.
(4) If a node is red, then both its children are black.
(5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
For example, the tree in Figure 1 is a red-black tree, while the ones in Figure 2 and 3 are not.
![]() |
![]() |
![]() |
Figure 1 | Figure 2 | Figure 3 |
For each given binary search tree, you are supposed to tell if it is a legal red-black tree.
Input Specification:
Each input file contains several test cases. The first line gives a positive integer K (<=30) which is the total number of cases. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the preorder traversal sequence of the tree. While all the keys in a tree are positive integers, we use negative signs to represent red nodes. All the numbers in a line are separated by a space. The sample input cases correspond to the trees shown in Figure 1, 2 and 3.
Output Specification:
For each test case, print in a line "Yes" if the given tree is a red-black tree, or "No" if not.
Sample Input:
3
9
7 -2 1 5 -4 -11 8 14 -15
9
11 -2 1 -7 5 -4 8 14 -15
8
10 -7 5 -6 8 15 -11 17
Sample Output:
Yes
No
No 题意:判断是否是红黑树。
红黑树定义:1:每个节点要么红要么黑2:根节点一定是黑的3:红色节点的子节点都是黑色4:任意一个节点的左右子树上的黑色节点数相同,其实就是根节点到叶子节点的所有路径上的黑色节点个数相同即可。
思路:已给出前序遍历,按顺序插入二叉树,构建搜索树。之后判断是否满足红黑树的定义。即对2,3,4三点定义进行判断。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 30000+5
typedef long long ll;
struct Node {
int key;
Node* left, *right;
};
Node *NIL,*root;
int t,n;
void insert(Node* &root,int key) {
if (root == NIL) {
root = new Node;
root->key = key;
root->left = root->right = NIL;
return;
}
if (abs(key) > abs(root->key))
insert(root->right, key);
else
insert(root->left, key);
}
bool is_correct = ;
bool is_first = ; int tot_num = ;
void judge(Node* root,int num) {
if (root == NIL) {
if (!is_first) {
tot_num = num;
is_first = ;
}
else if (num != tot_num)is_correct = false;
return;
}
if (root->key < ) {//当前节点是红色
if ((root->left != NIL&&root->left->key < ) || (root->right != NIL&&root->right->key < )) {
is_correct = false; return;
}
judge(root->left,num);
judge(root->right,num);
}
else {//当前黑色节点,num数量加1
judge(root->left, num+);
judge(root->right, num+);
}
} int main() {
scanf("%d", &t);
while (t--) {
root = NIL; is_first = ,tot_num = ,is_correct = true;
scanf("%d",&n);
for (int i = ; i < n;i++) {
int data; scanf("%d",&data);
if (!i&&data < )is_correct = false;
insert(root,data);
}
if (!is_correct) { printf("No\n"); continue; }
judge(root, );
if (is_correct)puts("Yes");
else puts("No");
}
}
pat 甲级 1135. Is It A Red-Black Tree (30)的更多相关文章
- 【PAT甲级】1099 Build A Binary Search Tree (30 分)
题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...
- 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 分)
我先在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甲级1135 Is It A Red-Black Tree?【dfs】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 题意: 给定一棵二叉搜索树的先序遍历结 ...
- 【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 ( ...
随机推荐
- Vue 恢复初始值的快速方法
vue 中经常定义很多data ,在用户进行一些操作后,需要讲data中的某个对象定义为初始值 例如 form: { title: '', describe: '', inspectionCatego ...
- 转:CentOS7 下 Redis4 安装与配置教程(Redis开机启动)
转 https://ken.io/note/centos7-redis4-setup 一.前言 1.本教程主要内容 Redis安装与测试 Redis远程访问配置 Redis开机启动配置 2.本教程环境 ...
- list变set去重,set交集
set 取交集 并集 删除没有的元素 不会报错 remove 会报错 https://www.cnblogs.com/alex3714/articles/5717620.html
- linux通配符知识
注意:linux通配符和三剑客(grep,awk,sed)正则表达式是不一样的,因此,代表的意义也是有较大区别的. 通配符一般用户命令行bash环境,而linux正则表达式用于grep,sed,awk ...
- Mybatis中的增删改查
相比jdbc mybatis在操作数据库方面比jdbc节省了大量的代码,及大量的代码冗余.使得操作起来更加简洁. 在Mapper中分别有着 select,insert, update,delete的这 ...
- 【解决】ERROR in xxx.js from UglifyJs
当我们运行打包脚本npm run build或者打包iosweexpack build ios有可能会遇到以下报错 ERROR in index.js from UglifyJs 当中. Vuejs中关于computed.m ...
- 05.VUE学习之表达式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- Python中的正则
regex_lst = [ ('字符组',), ('非打印字符',), ('特殊字符',), ('定位符',), ('限定符',), ('re模块',), ('分组命名',), ('或匹配',), ( ...