后序遍历数组: 左子树--右子树--根节点
取根节点, 从从左向右遍历数组段, 以第一个大于根节点的元素的索引(k)分割数组段
若右子树存在小于根节点的元素则false
class Solution {
public boolean verifyPostorder(int[] postorder) {
if(postorder==null || postorder.length==0) return true;
return function(postorder,0,postorder.length-1);
}
private boolean function(int[] arr,int i,int j){
if(i>j) return true;
int root=arr[j];
int k=i;//k为大于根节点的第一个节点的下标
for(;k<j;k++){
if(arr[k]>root) break;
}
for(int a=k;a<j;a++){
if(arr[a]<root) return false;
}
boolean m=true,n=true;
if(k>i) m=function(arr,i,k-1);
if(k<j-1) n=function(arr,k,j-1);
return m&&n;
}
}