package leetcode;
public class offer_33 {
public boolean verifyPostorder(int[] postorder) {
return isPost(postorder, 0, postorder.length-1);
}
public boolean isPost(int[] postorder,int left,int right) {
if(left>=right) {return true;}
int temp=left;
//找出当前节点的左右子树分界位置
while(postorder[temp]<postorder[right]){
temp=temp+1;
}
//记录分界位置
int mid=temp;
while(postorder[temp]>postorder[right]) {
temp=temp+1;
}
//temp==right说明符合二叉搜索树
//递归检查左右子树是否符合
return temp==right&&isPost(postorder, left, mid-1)&&isPost(postorder, mid, right-1);
}
}