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 ...
随机推荐
- kubeadm部署一个Kubernetes集群
kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具.这个工具能通过两条指令完成一个kubernetes集群的部署: # 创建一个 Master 节点 $ kubeadm in ...
- charles抓取数据
charles抓包 抓取手机app的数据 charles设置 help--->SSL Proxying--->Install Charles Root Certificate 1.给电脑下 ...
- delphi xe10 传感器操作
MotionSensor1: TMotionSensor; 加速传感器 MotionSensor1.Sensor(AngleAccelX.AngleAccelY.AngleAccelZ)加速度 pro ...
- 水一帖:快速计算ceil(a/b)的方式
今天拍脑袋想出来的,不用ceil函数,不用浮点运算,不用取模,兼顾运行常数和代码量的向上取整除方法 在保证a,b>0时 ceil(a/b)=(a-1)/b+1; (完)
- Python中的startswith和endswith函数使用实例
Python中的startswith和endswith函数使用实例 在Python中有两个函数分别是startswith()函数与endswith()函数,功能都十分相似,startswith()函数 ...
- PAT_A1064#Complete Binary Search Tree
Source: PAT A1064 Complete Binary Search Tree (30 分) Description: A Binary Search Tree (BST) is recu ...
- RHEL7更换yum源
1,删除注册和订阅提示 查找subscription-manager相关组件: rpm -qa | grep subscription-manager 删除subscription-maanager相 ...
- centos安装完php-fpm、nginx后访问网站出现权限问题
nginx.conf www.conf 这两个文件上面改用户为www. 如果不知道自己的配置文件位置问题,使用命令查找文件位置: find / -name 'nginx.conf' -print 添加 ...
- spark-submit 应用程序第三方jar文件
第一种方式:打包到jar应用程序 操作:将第三方jar文件打包到最终形成的spark应用程序jar文件中 应用场景:第三方jar文件比较小,应用的地方比较少 第二种方式:spark-submit 参数 ...
- jquery的attr获取表单checked 布尔值问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...