package java_study.JianZhiOffer;
import org.junit.Test;
/**
* Created by ethan on 2015/6/29.
* 剑指offer的 No24 二叉树后续遍历序列
* 剑指offer上的解答不够好, 在判断完左边之后, 如果发现左边为假,那么右边不应再递归调用, 可以提升性能
*/
public class No24二叉搜索树的后续遍历序列 {
public static boolean isPostOrderBinarySearchTree(String str, int start, int end){
if (str==null) return false;
if (start>end) return false;
boolean ans = true;
char root = str.charAt(str.length()-1);
int i=start;
for (; i<end; i++){
if (str.charAt(i)>root)
break;
}
for (int j=i; j<end; j++){
if (str.charAt(j)<root)
return false;
}
if (start<i){
ans = isPostOrderBinarySearchTree(str, start, i-1);
}
if (ans && i<end){
ans = isPostOrderBinarySearchTree(str, i, end-1);
}
return ans;
}
public static void main(String[] args){
String str = "1325764";
System.out.println(isPostOrderBinarySearchTree(str ,0 ,str.length()-1));
}
}
二叉搜索树的后续遍历序列
最新推荐文章于 2023-03-19 11:29:26 发布