public boolean verifyPostorder(int[] postorder) {
return verifyPostorderSub(postorder, 0, postorder.length-1);
}
private boolean verifyPostorderSub(int[] postorder, int lo, int hi) {
if (lo >= hi) return true;
int root = postorder[hi];
int idx = lo;
int idx1 = -1;
while (postorder[idx] < root) {
idx++;
}
idx1 = idx;
while (postorder[idx] > root){
idx++;
}
if (idx != hi) return false;
return verifyPostorderSub(postorder, lo, idx1-1) && verifyPostorderSub(postorder, idx1, hi-1);
}
剑指 Offer 33. 二叉搜索树的后序遍历序列
最新推荐文章于 2022-05-22 00:17:48 发布