数组切片
递归判断每一棵子树
对于每一棵子树,从最右端向左遍历,找到第一个小于最右节点的节点。符合搜索树规则的应该是,在该节点的左边所有的数都 小于最右结点,右边都大于最右结点
class Solution {
public boolean verifyPostorder(int[] postorder) {
return jud(postorder,0,postorder.length);
}
boolean jud(int[] postorder,int l,int r){
if(l >= r - 1){
return true;
}
int t = r - 2;
while(t >= l && postorder[t] > postorder[r - 1]) t--;
int j = l;
for(; j < t + 1; j++){
if(postorder[j] > postorder[r - 1])
return false;
}
for(; j < r - 1; j++){
if(postorder[j] < postorder[r - 1])
return false;
}
return jud(postorder,t + 1,r - 1) && jud(postorder,l,t + 1);
}
}