# 剑指offer--二叉搜索树的后序遍历序列

AC代码：

import java.util.Arrays;

public class Solution {
public boolean VerifyLeftTree(int[] leftsequence , int root){
boolean flag = true;
for ( int i = 0 ; i < leftsequence.length ; i++){
if ( leftsequence[i] >= root){
flag = false;
break;
}
}
return flag;
}

public boolean VerifyRightTree(int[] rightsequence , int root){
boolean flag = true;
for ( int i = 0 ; i < rightsequence.length ; i++){
if ( rightsequence[i] <= root){
flag = false;
break;
}
}
return flag;
}

//判断sequence是否已经有序
public boolean IsOrder(int [] sequence){
//判断是否已经是升序
int[] tmp = Arrays.copyOfRange(sequence, 0, sequence.length);
Arrays.sort(tmp);
boolean flag1 = true;
for(int i = 0 ; i < sequence.length ; i++){
if ( sequence[i] != tmp[i]){
flag1 = false;
break;
}
}

//判断是否已经是降序
int cnt = 0;
boolean flag2 = true;
for ( int i = tmp.length-1 ; i >= 0 ; i--){
if ( sequence[cnt++] != tmp[i]){
flag2 = false;
break;
}
}

boolean flag = flag1 || flag2;
return flag;
}

public boolean VerifySquenceOfBST(int [] sequence) {
int partition = 0;
int len = sequence.length;
if ( sequence == null || len == 0){
return false;
}

int root = sequence[len-1];
if ( IsOrder(sequence)){
return true;
}

for ( int i = 0 ; i < len-1 ; i++){
if (sequence[i] < root && sequence[i+1] > root){
partition = i;
break;
}
}

int[] left = Arrays.copyOfRange(sequence, 0, partition+1);
int[] right = Arrays.copyOfRange(sequence, partition+1, len-1);

for ( int i = 0 ; i < left.length ; i++){
System.out.print(left[i]+" ");
}
System.out.println();
for ( int i = 0 ; i < right.length ; i++){
System.out.print(right[i]+" ");
}
boolean leftflag = VerifyLeftTree(left,root);
boolean rightflag = VerifyRightTree(right,root);
boolean flag = leftflag & rightflag;
return flag;
}
}

#### 剑指offer 面试题24：二叉搜索树BST的后序遍历序列(的判断) 题解

2016-05-04 19:59:18

#### 剑指Offer:面试题6重建二叉树

2016-08-17 14:18:42

#### 二叉搜索树的后序遍历序列（Java实现）

2017-04-27 10:04:00

#### 二叉搜索树的后序遍历序列Java

2016-03-08 16:10:11

#### 剑指Offer-->二叉搜索树的后序遍历序列

2015-05-23 12:58:35

#### 剑指Offer面试题24（Java版）：二叉搜索树的后序遍历序列

2015-08-04 20:26:12

#### 判断二叉搜索树的后序遍历序列是否合法

2016-09-25 11:11:37

#### 【剑指Offer】二叉搜索树的后序遍历序列

2015-09-19 01:57:24

#### 剑指offer--面试题24:二叉搜索树的后序遍历序列--Java实现

2015-06-21 16:28:15

#### 二叉搜索树的后序遍历序列（递归与非递归）

2016-03-25 17:38:02