方法一:递归+分治
后续遍历: [[左] [右] [根]] 满足左<根<右
class Solution {
public:
bool isPos(vector<int>& postorder,int left,int right){
if(left>=right) return true;
int j = left;
while(j<right&&postorder[j]<postorder[right]){
j++;
}
for(int i = j;i<right;i++){
if(postorder[i]<postorder[right]) return false;
}
return isPos(postorder,left,j-1)&&isPos(postorder,j,right-1);
}
bool verifyPostorder(vector<int>& postorder) {
int size = postorder.size();
return isPos(postorder,0,size-1);
}
};
该文章介绍了一种方法,通过递归和分治策略来检查给定的后序遍历序列是否合法。具体实现是通过后续遍历的特性[[左][右][根]],在postorder数组中找到分界点,然后对左右子序列分别进行递归验证。
520

被折叠的 条评论
为什么被折叠?



