对A1135这题有心里阴影了,今天终于拿下AC。学习自柳神博客:https://www.liuchuo.net/archives/4099

首先读题很关键:

There is a kind of balanced binary search tree named red-black tree in the data structure………………

红黑树首先应该是一棵BST树,不然从何谈起维护二分查找结构?

所以第一步就应该根据先序遍历以及BST树的特性来判断是否是一棵BST树,然后根据这两个条件生成树形链式结构。


柳神博客中给出的方法是生成后序遍历然后判断size是否与先序遍历的长度相等。笔者认为生成一个新的先序遍历然后判断长度也可以。

void getPost(int root,int end){//create a BST by pre order and BST's property
if(root>end) return;
int i=root+,j=end;
while(i<=end && pre[root]>pre[i]) i++;
while(j>=root+ && pre[root]<pre[j]) j--;
if(i!=j+) return;
getPost(root+,j);
getPost(i,end);
post.push_back(pre[root]);
}

以上是生成后序遍历的代码。代码逻辑:

7 2 1 5 4 11 8 14 16

j↑ ↑i

让i->遍历[root + 1 , 大于root的数],j<-遍历[小于root的数 , end]

然后二者相互错开,建立新的递归关系。

特殊边界条件:

  \

   2

     \

      3

这个结构符合BST树的定义,先序遍历是 123。

第一轮:

1 2 3

j i

第二轮:

2 3

j i

第三轮:

ij


然后再谈到递归判断。两个递归判断代码:

int getNum(Node* node){
if(!node) return ;
int l=getNum(node->l);
int r=getNum(node->r);
int m=max(l,r);
if(!node->isR) m++;
return m;
}
bool judge1(Node* node){//every path to leaves have same black
if(!node) return true;
if(getNum(node->l)!=getNum(node->r)) return false;
return judge1(node->l) && judge1(node->r);
}
bool judge2(Node* node){//the node is red , the both leaves is black
if(!node) return true;
if(node->isR){
if(node->l && node->l->isR) return false;
if(node->r && node->r->isR) return false;
}
return judge2(node->l) && judge2(node->r);
}

完整代码:

#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <map> #define I scanf
#define OL puts
#define O printf
#define F(a,b,c) for(a=b;a<c;a++)
#define FF(a,b) for(a=0;a<b;a++)
#define FG(a,b) for(a=b-1;a>=0;a--)
#define LEN 10000
#define MAX 0x06FFFFFF
#define V vector<int> using namespace std; V post;
V pre;
typedef struct Node{
int d=;
bool isR=true;
struct Node* l=NULL;
struct Node* r=NULL;
Node(int D,int I){
d=D;isR=I;
l=NULL;r=NULL;
}
}Node; Node *root=NULL; void getPost(int root,int end);
Node* bst_insert(Node* p,int d);
bool judge1(Node* node);
bool judge2(Node* node); int main() {
freopen("d:/input/A1135/1.txt","r",stdin);
int N,M,i;
scanf("%d",&N);
while(N-->){
scanf("%d",&M);
pre.resize(M);
post.clear();
root=NULL;
FF(i,M){
int t;
scanf("%d",&t);
pre[i]=abs(t);
root=bst_insert(root,t);
}
getPost(,M-);
if(post.size()!=M){
OL("No");continue;
}
if(root->isR){
OL("No");continue;
}
if(judge1(root) && judge2(root))
OL("Yes");
else OL("No");
}
// OL("OK");
return ;
} void getPost(int root,int end){//create a BST by pre order and BST's property
if(root>end) return;
int i=root+,j=end;
while(i<=end && pre[root]>pre[i]) i++;
while(j>=root+ && pre[root]<pre[j]) j--;
if(i!=j+) return;
getPost(root+,j);
getPost(i,end);
post.push_back(pre[root]);
} Node* bst_insert(Node* p,int d){
Node* node=new Node(abs(d),d<);//if d < 0 , d is red
if(!p){//node is null
return node;
}else{
int v=abs(d);
if(v>p->d){//r
if(p->r){
p->r=bst_insert(p->r,d);
}else{
p->r=node;
}
}else{
if(p->l){
p->l=bst_insert(p->l,d);
}else{
p->l=node;
}
}
}
return p;
} int getNum(Node* node){
if(!node) return ;
int l=getNum(node->l);
int r=getNum(node->r);
int m=max(l,r);
if(!node->isR) m++;
return m;
} bool judge1(Node* node){//every path to leaves have same black
if(!node) return true;
if(getNum(node->l)!=getNum(node->r)) return false;
return judge1(node->l) && judge1(node->r);
} bool judge2(Node* node){//the node is red , the both leaves is black
if(!node) return true;
if(node->isR){
if(node->l && node->l->isR) return false;
if(node->r && node->r->isR) return false;
}
return judge2(node->l) && judge2(node->r);
}

A1135 | 红黑树判断:审题、根据“先序遍历”和“BST树”的条件生成后序遍历、递归判断的更多相关文章

  1. PAT甲题题解-1119. Pre- and Post-order Traversals (30)-(根据前序、后序求中序)

    (先说一句,题目还不错,很值得动手思考并且去实现.) 题意:根据前序遍历和后序遍历建树,输出中序遍历序列,序列可能不唯一,输出其中一个即可. 已知前序遍历和后序遍历序列,是无法确定一棵二叉树的,原因在 ...

  2. 数据结构与算法--从平衡二叉树(AVL)到红黑树

    数据结构与算法--从平衡二叉树(AVL)到红黑树 上节学习了二叉查找树.算法的性能取决于树的形状,而树的形状取决于插入键的顺序.在最好的情况下,n个结点的树是完全平衡的,如下图"最好情况&q ...

  3. 【Java入门提高篇】Day25 史上最详细的HashMap红黑树解析

    当当当当当当当,好久不见,最近又是换工作,又是换房子,忙的不可开交,断更了一小段时间,最重要的一篇迟迟出不来,每次都犹抱琵琶半遮面,想要把它用通俗易懂的方式进行说明,确实有一定的难度,可愁煞我也,但自 ...

  4. AVL树与红黑树

    平衡树是平时经常使用数据结构. C++/JAVA中的set与map都是通过红黑树实现的. 通过了解平衡树的实现原理,可以更清楚的理解map和set的使用场景. 下面介绍AVL树和红黑树. 1. AVL ...

  5. Java集合详解6:TreeMap和红黑树

    Java集合详解6:TreeMap和红黑树 初识TreeMap 之前的文章讲解了两种Map,分别是HashMap与LinkedHashMap,它们保证了以O(1)的时间复杂度进行增.删.改.查,从存储 ...

  6. 史上最详细的HashMap红黑树解析

      简介:请允许我当一回标题党.好了,言归正传,本篇主要内容便是介绍HashMap的男二号——TreeNode(男一号还是给Node吧,毕竟是TreeNode的爷爷,而且普通节点一般来说也比TreeN ...

  7. Bzoj3227 [Sdoi2008]红黑树(tree)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 204  Solved: 125 Description 红黑树是一类特殊的二叉搜索树,其中每个结点被染 ...

  8. 算法导论 第十三章 红黑树(python)-1插入

    红黑树是上一章二叉搜索树的改进,实现一种平衡 ,保证不会出现二叉树变链表的情况,基本动态集合操作的时间复杂度为O(lgn) 实际用途:c++stl中的set,map是用他实现的 红黑树的性质: 1.每 ...

  9. 红黑树之 原理和算法详细介绍(阿里面试-treemap使用了红黑树) 红黑树的时间复杂度是O(lgn) 高度<=2log(n+1)1、X节点左旋-将X右边的子节点变成 父节点 2、X节点右旋-将X左边的子节点变成父节点

    红黑树插入删除 具体参考:红黑树原理以及插入.删除算法 附图例说明   (阿里的高德一直追着问) 或者插入的情况参考:红黑树原理以及插入.删除算法 附图例说明 红黑树与AVL树 红黑树 的时间复杂度 ...

随机推荐

  1. OpenGL学习 (一) - 简单窗口绘制

    一.OpenGL 简介 OpenGL 本质: OpenGL(Open Graphics Library),通常可以认为是API,其包含了一系列可以操作图形.图像的函数.但深究下来,它是由Khronos ...

  2. php 安装imap报错“configure: error: utf8_mime2text() has new signature”解决

    环境:php官方docker镜像 php:7.2-apache 安装IMAP扩展模块执行命令:docker-php-ext-install imap 报错信息:configure: error: ut ...

  3. element-UI级联选择器(Cascader)获取label值 ,this.$refs['新组件名'].currentLabels 在2.7版本给移除了,新的解决方法。

    原文参考:https://blog.csdn.net/lijiabinbbg/article/details/97396812 遇到的新的问题是如果设置了ref,那么v-model绑定的值不会动态更新 ...

  4. Python面向对象之私有属性和私有方法

    01. 应用场景及定义方式 应用场景 在实际开发中,对象 的 某些属性或方法 可能只希望 在对象的内部被使用,而 不希望在外部被访问到 私有属性 就是 对象 不希望公开的 属性 私有方法 就是 对象  ...

  5. 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)

    题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...

  6. BUAA-OO-2019 第一单元总结

    第一次作业 第一次作业需要完成的任务为简单多项式导函数的求解. 思路 因为仅仅是简单多项式的求导,所以求导本身没有什么可说的,直接套用幂函数的求导公式就行了,主要的精力是花在了正则表达式上.这里推荐两 ...

  7. Computer Networking: A Top Down Approach

    目录 Chapter 1: Computer Networks and the Internet 1. What is the Internet? 2. The Network Edge 3. The ...

  8. js获取某月有多少天

    var day = new Date(2018,10,0); //最后一个参数为0,意为获取2018年10月一共多少天 console.log(day.getDate());

  9. java 判断虚拟网卡物理网卡

    读取注册表方式,jregistrykey.jar与jregistrykey.dll.通过“characteristics”值确定虚拟网卡还是物理网卡.该值在注册表的位置HKEY_LOCAL_MACHI ...

  10. Jmeter如何测试接口

    现在对测试人员的要求越来越高,不仅仅要做好功能测试,对接口测试的需求也越来越多!所以也越来越多的同学问,怎样才能做好接口测试? 要真正的做好接口测试,并且弄懂如何测试接口,需要从如下几个方面去分析问题 ...