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】的更多相关文章

  1. 【PAT甲级】1099 Build A Binary Search Tree (30 分)

    题意: 输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值.输出这颗二叉排序树的层次遍历. AAAAAccepted code: # ...

  2. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  3. PAT A1135 Is It A Red Black Tree

    判断一棵树是否是红黑树,按题给条件建树,dfs判断即可~ #include<bits/stdc++.h> using namespace std; ; struct node { int ...

  4. 【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 ...

  5. PAT甲级1123 Is It a Complete AVL Tree【AVL树】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...

  6. PAT 甲级 1043 Is It a Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. tomcat 端口被占用 项目端口号被占用怎么解决

    1.Win+R  打开运行 ,输入cmd 打开命令行窗口 . 2.假设要查询端口被占用情况,在命令行下输入:netstat  -aon|findstr  "8883" 3.得到进程 ...

  2. Servlet中的Filter怎么使用?

    一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...

  3. python编程语言学习day05(1)

    模块(不起与python相关模块的名字) 1.random random.random()    在0与1之间取随机数 random.randint(a,b)  在a,b之间取随机整数,包括边界 ra ...

  4. tomcat部署安全证书文件(阿里云SSL证书)

    1.下载安全证书文件: 这里使用的是阿里云SSL证书(免费一年) 2.把下载的压缩包进行解压 3.将pfx文件拷贝至服务器 4.利用jdk将pfx转jks 5.cmd进入命令行 6.切换至jdk的bi ...

  5. BZOJ 4517: [Sdoi2016]排列计数(组合数学)

    题面 Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m ...

  6. NX二次开发-UFUN建模创建特征组UF_MODL_create_set_of_feature

    NX11+VS2013 #include <uf.h> #include <uf_modl.h> UF_initialize(); //创建块 UF_FEATURE_SIGN ...

  7. CSS3:CSS3 渐变(Gradients)

    ylbtech-CSS3:CSS3 渐变(Gradients) 1.返回顶部 1. CSS3 渐变(Gradients) CSS3 渐变(gradients)可以让你在两个或多个指定的颜色之间显示平稳 ...

  8. c++-文件分离

    实现文件分离 1.头文件中不要使用using namespace,由于c++编译的特性,由于初学还没深入了解,不做具体编译的解释 2.由于没有了命名空间,所以string定义要写成std::strin ...

  9. 注意:字符串substring方法在jkd6,7,8中的差异。

    标题中的substring方法指的是字符串的substring(int beginIndex, int endIndex)方法,这个方法在jdk6,7是有差异的. substring有什么用? sub ...

  10. eclipse新建maven项目和聚合项目

    1.new maven project :  next 2.勾选 create a simple project  :  next 3.Group Id:项目的包路径 如com.jiayou.zjl, ...