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 ...
随机推荐
- LUOGU P1501 [国家集训队]Tree II (lct)
传送门 解题思路 \(lct\),比较模板的一道题,路径加和乘的维护标记与线段树\(2\)差不多,然后剩下就没啥了.但调了我将近一下午.. 代码 #include<iostream> #i ...
- How to SSH Into Your iPhone
First, I will explain what SSH is and why we do it. SSH (Secure Shell) allows you to exchange data b ...
- C++之string
一.常用操作 二.用“+”连接字符串的注意事项
- class4_Listbox 列表部件
最终的运行效果图: #!/usr/bin/env python# -*- coding:utf-8 -*-# --------------------------------------------- ...
- LightOJ-1282-Leading and Trailing-快速幂+数学
You are given two integers: n and k, your task is to find the most significant three digits, and lea ...
- 如何提高阿里云cdn命中率?原命中率极低。
博客搬迁了,给你带来的不便,敬请谅解! http://www.suanliutudousi.com/2017/08/29/%E5%A6%82%E4%BD%95%E6%8F%90%E9%AB%98%E9 ...
- mysql8以上版本时区问题:The server time zone value乱码XXXX
异常类似: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. ...
- 19-MySQL-Ubuntu-数据表的查询-自关联(八)
自关联 转自:https://blog.csdn.net/hubingzhong/article/details/81277220
- HDU 3308 线段树求区间最长连续上升子序列长度
题意:两种操作,Q L R查询L - R 的最长连续上升子序列长度,U pos val 单点修改值 #include <bits/stdc++.h> #define N 100005 us ...
- 2018ICPC焦作 F. Honeycomb /// BFS
题目大意: 给定n m表示一共n行每行m个蜂巢 求从S到T的最短路径 input 1 3 4 +---+ +---+ / \ / \ + +---+ +---+ \ \ / \ + + S +---+ ...