二叉搜索树的后序遍历序列
利用二叉搜索树的定义,左子树值小于根节点,根节点小于右子树节点值。后序遍历根节点在结尾,从头查找小于根节点区间,大于根节点去区间。
public static boolean verifyPostorder(int[] postorder) {
//[1, 2, 3, 5, 6] [1,6,3,2,5] [1,3,2,6,5]
return verify(postorder , 0, postorder.length - 1);
}
public static boolean verify(int[] postorder, int i, int j) {
if (i >= j) return true;
int val = postorder[j];
int x = i;
while (postorder[x] < val) x++;
int m = x;
while (postorder[x] > val) x++;
return x == j && verify(postorder, i, m - 1) && verify(postorder, m, j - 1);
}