PAT-A1135. Is It A Red-Black Tree (30)
已知先序序列,判断对应的二叉排序树是否为红黑树。序列中负数表示红色结点,正数表示黑色结点。该序列负数取绝对值后再排序得到的是中序序列。根据红黑树的性质判断它是否符合红黑树的要求。考察了根据先序序列和中序序列建树和DFS。
//#include "stdafx.h"
#include <iostream>
#include <algorithm> using namespace std; struct treeNode { // tree node struct
int key, lchild, rchild, flag; // key, left child, right child, color flag
}tree[]; struct intNode { // int node
int key, flag; // key, color flag
}pre[]; // the array of the preorder traversal sequence int treeRoot, flag, blackNodeCount, in[]; // the index of tree root node, whether it is a red-black tree, the number of black nodes, the array of the inorder traversal sequence void init(int m) { // initialize
int i;
for (i = ; i < m; i++) { // every node has no child
tree[i].lchild = tree[i].rchild = -;
} flag = ; // at first, it is a red-black tree
treeRoot = blackNodeCount = ; // the initial index of tree root node is zero and the initial number of black nodes is zero sort(in, in + m); // sort in array to get the inorder traversal sequence
} int buildTree(int a1, int a2, int b1, int b2) { // build a tree according to the preorder traversal sequence and inorder
// initialize the current root node of the subtree
int root = treeRoot++;
int key = pre[a1].key;
tree[root].key = key;
tree[root].flag = pre[a1].flag; int i;
for (i = b1; i <= b2; i++) { // seek the index of root node in the inorder traversal sequence
if (in[i] == key) {
break;
}
} if (i > b1) { // if left subtree exists
tree[root].lchild = buildTree(a1 + , a1 + i - b1, b1, i - );
}
if (i < b2) { // if right subtree exists
tree[root].rchild = buildTree(a1 + i - b1 + , a2, i + , b2);
} return root; // return the current root node of the subtree
} void dfs(int cur, int count) { // traverse all nodes based on depth first
if (flag == ) { // if it is not a red-black tree
return;
} if (cur == -) { // if it is a leaf node
if (count != blackNodeCount) { // judge whether black nodes is legal
if (blackNodeCount == ) {
blackNodeCount = count;
} else {
flag = ;
}
} return;
} int l = tree[cur].lchild;
int r = tree[cur].rchild; if (tree[cur].flag == ) { // If a node is red, then both its children are black.
if (l != - && tree[l].flag == ) {
flag = ;
return;
} else if (r != - && tree[r].flag == ) {
flag = ;
return;
}
} else {
count++;
} // recursion
dfs(l, count);
dfs(r, count);
} int judgeRBTree() {
if (tree[].flag == ) { // The root is black.
return ;
} // traverse
dfs(, );
return flag;
} int main() {
int n;
scanf("%d", &n); int m, i;
while (n--) {
scanf("%d", &m);
for (i = ; i < m; i++) {
scanf("%d", &pre[i].key); // save the information of color
if (pre[i].key < ) {
pre[i].key = - pre[i].key;
pre[i].flag = ;
} else {
pre[i].flag = ;
} in[i] = pre[i].key;
} init(m); // initialization
buildTree(, m - , , m - ); // build a tree if (judgeRBTree() == ) {
printf("Yes\n");
} else {
printf("No\n");
}
} system("pause");
return ;
}

PAT-A1135. Is It A Red-Black Tree (30)的更多相关文章
- PAT A1135 Is It A Red Black Tree
判断一棵树是否是红黑树,按题给条件建树,dfs判断即可~ #include<bits/stdc++.h> using namespace std; ; struct node { int ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 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)
1135. Is It A Red-Black Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- pat 甲级 1099. Build A Binary Search Tree (30)
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- PAT Advanced 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 ...
- 【PAT甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- PAT Advanced 1151 LCA in a Binary Tree (30) [树的遍历,LCA算法]
题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...
- PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
随机推荐
- 【转】snprintf()函数使用方法
众所周知,sprintf不能检查目标字符串的长度,可能造成众多安全问题,所以都会推荐使用snprintf. 注:sprintf()函数:int sprintf( char *buffer, const ...
- 【bzoj2653】【middle】【主席树+二分答案】
Description 一个长度为 n 的序列 a ,设其排过序之后为 b ,其中位数定义为 b[n/2] ,其中 a,b 从 0 开始标号 , 除法取下整. 给你一个长度为 n 的序列 s .回答 ...
- phpstudy中apache的默认根目录的配置
默认配置文件是:vhosts.conf. 安装laravel后需要把根目录配置到public. 下面的配置需要在本地计算机的host文件配置域名,一个是“localhost”,一个是“www.goho ...
- 生产环境elasticsearch5.0.1和6.3.2集群的部署配置详解
线上环境elasticsearch5.0.1集群的配置部署 es集群的规划: 硬件: 7台8核.64G内存.2T ssd硬盘加1台8核16G的阿里云服务器 其中一台作为kibana+kafka连接查询 ...
- CentOS 6.5环境使用ansible剧本自动化部署Corosync + pacemaker环境及corosync常用配置详解
环境说明: 192.168.8.39 node2.chinasoft.com 192.168.8.42 node4.chinasoft.com 192.168.8.40 ansible管理服务器 19 ...
- spring上传文件
在使用spring上传文件的时候,使用的文件接收参数类型为 org.springframework.web.multipart.MultipartFile 如果该参数没有指定@RequestParam ...
- Python-浮动 清浮动
# 浮动布局 ## 一.display总结 ```css/* inline *//*1.同行显示, 就相当于纯文本, 当一行显示不下, 如就是一个字显示不下,那么显示不下的那一个字就会自动换行,和纯文 ...
- JS实现购物车02
需求使用JS实现购物车功能02 具体代码 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- MACE(2)-----模型编译
作者:十岁的小男孩 QQ:929994365 无用 本文仅用于学习研究,非商业用途,欢迎大家指出错误一起学习,文章内容翻译自 MACE 官方手册,记录本人阅读与开发过程,力求不失原意,但推荐阅读原文. ...
- RzPageControl Tab拖拽 移动