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 ...
随机推荐
- CentOS 6.5下PXE+Kickstart无人值守安装操作系统centos7.3
CentOS 6.5下PXE+Kickstart无人值守安装操作系统centos7.3 一.简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行 ...
- Java Map 键值对排序 按key排序和按Value排序
一.理论准备 Map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等. TreeMap:基于红黑树(Red-Black tre ...
- windows安装配置git和Tortoisegit
git github gitlab Tortoisegit 的概念自行百度 1. 安装git 2. 安装小乌龟:Tortoisegit 和中文包 3. 配置 4. 使用 参考: 目录 安装及配置 ...
- Python-垃圾回收机制
引子: 我们定义变量会申请内存空间来存放变量的值,而内存的容量是有限的,当一个变量值没有用了(简称垃圾)就应该将其占用的内存给回收掉,而变量名是访问到变量值的唯一方式,所以当一个变量值没有关联任何变量 ...
- 基于Jenkins,docker实现自动化部署(持续交互)【转】
前言 随着业务的增长,需求也开始增多,每个需求的大小,开发周期,发布时间都不一致.基于微服务的系统架构,功能的叠加,对应的服务的数量也在增加,大小功能的快速迭代,更加要求部署的快速化,智能化.因此 ...
- xpath定位
XML 实例文档 我们将在下面的例子中使用这个 XML 文档. <?xml version="1.0" encoding="ISO-8859-1"?> ...
- SpringMVC(4.2):Controller接口控制器详解(2)
原文出处: 张开涛 4.5.ServletForwardingController 将接收到的请求转发到一个命名的servlet,具体示例如下: package cn.javass.chapter4. ...
- IE 浏览器 GET 请求缓存问题
问题描述 IE 浏览器(笔者使用的版本是 IE 11)在发起 GET 请求,当参数一样时,浏览器会直接使用缓存数据,这样对于实时性有要求的数据不适用.笔者在使用 Chrome 或 FF 时发现浏览器并 ...
- 洛谷P1970 花匠
传送门 首先可以知道,如果一个序列是连续上升的,那么只需要取这一个序列中最高的元素即可,因为取其它的不能保证大于后面的.连续下降的序列同理.而这些恰好就是波峰和波谷. 所以遇到 $ j $ 比之前的 ...
- 内联外联CSS和JS
内联CSS 代码示例: <p style="color:red;font-size:18px">这里文字是红色.</p> 内联CSS也可称为行内CSS或者行 ...