PAT甲级——A1135 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.从任意结点到叶子结点的路径中,黑色结点的个数是否相同
所以分为以下几步:
0. 根据先序建立一棵树,用链表表示
1. 判断根结点(题目所给先序的第一个点即根结点)是否是黑色
2. 根据建立的树,从根结点开始遍历,如果当前结点是红色,判断它的孩子节点是否为黑色,递归返回结果
3. 从根节点开始,递归遍历,检查每个结点的左子树的高度和右子树的高度(这里的高度指黑色结点的个数),比较左右孩子高度是否相等,递归返回结果
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
struct Node
{
int val;
Node *l, *r;
Node(int a) :val(a), l(nullptr), r(nullptr) {}
};
int n, m;
int getHigh(Node *root)//是指黑节点个数哦
{
if (root == nullptr)
return ;
int ln = getHigh(root->l);
int rn = getHigh(root->r);
return root->val > ? max(ln, rn) + : max(ln, rn);//计算黑节点个数
}
Node *Insert(Node *root, int x)
{
if (root == nullptr)
root = new Node(x);
else if (abs(x) < abs(root->val))
root->l = Insert(root->l, x);
else
root->r = Insert(root->r, x);
return root;
}
bool redNode(Node *root)
{
if (root == nullptr)
return true;
if (root->val < )//红节点孩子一定要是黑节点
if (root->l != nullptr && root->l->val < ||
root->r != nullptr && root->r->val < )
return false;
return redNode(root->l) && redNode(root->r);
}
bool balanceTree(Node *root)
{
if (root == nullptr) return true;
if (getHigh(root->l) != getHigh(root->r))return false;//黑节点个数不同
return balanceTree(root->l) && balanceTree(root->r);
}
int main()
{
int n, m;
cin >> n;
while (n--)
{
cin >> m;
vector<int>v(m);
Node *root = nullptr;
for (int i = ; i < m; ++i)
{
cin >> v[i];
root = Insert(root, v[i]);
}
if (v[] >= && balanceTree(root) && redNode(root))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return ;
}
PAT甲级——A1135 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 A1135 Is It A Red Black Tree
判断一棵树是否是红黑树,按题给条件建树,dfs判断即可~ #include<bits/stdc++.h> using namespace std; ; struct node { int ...
- 【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 ...
- PAT 甲级 1043 Is It a Binary Search Tree (25 分)(链表建树前序后序遍历)*不会用链表建树 *看不懂题
1043 Is It a Binary Search Tree (25 分) A Binary Search Tree (BST) is recursively defined as a bina ...
- PAT甲级——A1123 Is It a Complete AVL Tree【30】
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
随机推荐
- 基于 CI 1.7.x 的 项目使用新版本CI的文件缓存类库
维护的项目使用的是 codeigniter 1.7.x版本,但是我想使用文件缓存,但是旧版本是没有缓存类库的,并且autoload.php没有drivers这个配置项. 我复制的是 Codeignit ...
- Linux基本使用命令
一.常用命令归纳分类 课外网站 http://man.linuxde.net/ http://www.jb51.net/linux/ http ...
- 整理delphi及整理原则
回看delphi使用的人也不多,但一直觉得这门语言挺好的,所以一直在用,在很多方面也给了很多帮助和启示 加上delphi的学习文件也确实比较少,故收集起来也不容易.今日,重新整理一下delphi ,一 ...
- nextJS使用注意事项
项目参考 nextJs-yicha 1. 采用方案 create-next-app.antd (1)安装 npx create-next-app --example with-ant-design m ...
- kafka的消息组件
kafka的组件的介绍 produer:消息的生产者,往topic当中生产消息 consumer:消息的消费者,从topic当中消费消息 broker:kafka的服务器 zookeeper:kafk ...
- NX二次开发-UFUN删除链表函数UF_MODL_delete_list
NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> #include <u ...
- vmware压缩磁盘空间的方法, linux怎么卸载vmware
家里笔记本40G,可用空间怎么挤都只剩7G,从单位copy回来的linux虚拟机要10G,经检查实际使用空间5.7,也就是其他都是空余空间,可以释放掉.(只适合independent.Persiste ...
- LeetCode 2. Add Two Numbers (两数相加)
题目标签:Linked List, Math 题目给了我们两个 Linked List, 各代表一个数字,不过顺序的反的.让我们把两个数字相加. 和普通的相加其实差不多,只不过变成了 Linked L ...
- linux安装&卸载IB网卡(mellanox)驱动
由于工作上的需求,需要在redhat6.5服务器上面安装mellanox品牌的IB网卡,故整理了一个安装&卸载IB网卡驱动的教程: 一.安装 1)下载相应操作系统驱动 http://www.m ...
- JVM内核-原理、诊断与优化学习笔记(一):初识JVM
文章目录 JVM的概念 JVM是Java Virtual Machine的简称.意为Java虚拟机 虚拟机 有哪些虚拟机 VMWare或者Visual Box都是使用软件模拟物理CPU的指令集 JVM ...