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 ...
随机推荐
- Laravel View Composer - 当 include 一个模板时,自动获取其所需的变量
网站中,许多页面的侧边栏是相同的.例如: 分类列表页,与文章详情页的侧边栏都包含 最新文章 最新评论 统计计数 这些相同的侧边栏数据也是动态的,并不是固定的. 在每个 controller 里都写一遍 ...
- python 全栈开发,Day136(爬虫系列之第3章-Selenium模块)
一.Selenium 简介 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全 ...
- python 全栈开发,Day133(玩具与玩具之间的对话,基于jieba gensim pypinyin实现的自然语言处理,打包apk)
先下载github代码,下面的操作,都是基于这个版本来的! https://github.com/987334176/Intelligent_toy/archive/v1.6.zip 注意:由于涉及到 ...
- HDU2873 Bomb Game(二维SG函数)
Bomb Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- SqlServer索引碎片
1.产生碎片的操作 通过sys.dm_index_physical_stats来查看,索引上的页不在具有连续性时就会产生碎片,碎片是索引上页拆分的物理结果. (1).插入操作: INSERT操作在聚集 ...
- centos java tomcat 中文乱码解决办法
现象: cenos 部署java web 程序 ,java类中有中文 出现乱码现象 即使使用: System.getProperty("中文") 控制台都出现 ?????? 乱 ...
- canvas百分百特效
这个特效是别的人,非原创.原创地址 http://blog.csdn.net/lecepin/article/details/53536445 背后的水是可以动的 代码我再研究了下,下面是加了注释的代 ...
- 6.Django与Ajax
Ajax 文件夹为Ajaxdemo 向服务器发送请求的途径: 1.浏览器地址栏,默认get请求: 2.form表单: get请求 post请求 3.a标签,超链接(get请求) 4.Ajax请求 特点 ...
- Java Swing提供的文件选择对话框 - JFileChooser
JFileChooser() 构造一个指向用户默认目录的 JFileChooser. JFileChooser(File currentDirectory) 使 ...
- java过滤防止sql注入过滤
/** * 过滤特殊字符 * @author: Simon * @date: 2017年8月31日 下午1:47:56 * @param str * @return */ public static ...