输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。
参考以下这颗二叉搜索树:
5
/ \
2 6
/ \
1 3
示例 1:
输入: [1,6,3,2,5]
输出: false
示例 2:
输入: [1,3,2,6,5]
输出: true
int[] pos;
public boolean verifyPostorder(int[] postorder) {
this.pos=postorder;
return dfs(0,pos.length-1);
}
// root 也可以理解为 right,因为是后序遍历数组
public boolean dfs(int left,int root){
if(root < left)return true;
// 找到第一个小于根节点的下标,这就是左子树的右边界
int firsetLess = root-1;
while(firsetLess >=0 && pos[firsetLess]>pos[root]){
firsetLess--;
}
// 遍历左子树
for(int i = firsetLess;i>=0;i--){
if(pos[i]>pos[root])return false;
}
return dfs(firsetLess+1,root-1) && dfs(left,firsetLess);
}
public boolean verifyPostorder(int[] postorder) {
return recur(postorder, 0, postorder.length - 1);
}
boolean recur(int[] postorder, int i, int j) {
if(i >= j) return true;
int p = i;
// 划分左子树
while(postorder[p] < postorder[j]) p++;
// m,p 为右子树起点
int m = p;
// 只要右子树合法 p 最终就会遍历到根节点,然后 postorder[p] == postorder[j]) 就结束循环了
while(postorder[p] > postorder[j]) p++;
// 所以最终结果就是 p 是否等于 j 以及递归进行左右子树的判断
return p == j && recur(postorder, i, m - 1) && recur(postorder, m, j - 1);
}
public boolean verifyPostorder(int[] postorder) {
Stack<Integer> stack = new Stack<>();
// 初始根节点是没有根节点的,所以为了最开始是符合条件的
// 那么我们就用一定符合条件的最大值默认为他的根节点
// 因为我们是根据判断左子树来验证是否为搜索树
// 所以也就等同于我们默认根节点为一个节点的左节点,那这个结点自然是要一个一定大于根节点的数
int root = Integer.MAX_VALUE;
for(int i = postorder.length - 1; i >= 0; i--) {
// 若当前数大于根节点就有问题了
if(postorder[i] > root) return false;
// 只要突然递减了就会进入循环寻找根节点 root
// 找到找完或者存储的数小于当前节点为止
// 这也就代表我们找到了最接近当前节点的值并且大于这个值的节点
// 也就是根节点
// 也是由于整体趋势是根右左,大体呈现为降序
// 所以我们顺序遍历的整体趋势也同样是从大往小遍历
// 所以在验证的过程中 root 也是越来越小的
while(!stack.isEmpty() && stack.peek() > postorder[i])
root = stack.pop();
// 把点都 push 进去,以备之后寻找 root
stack.add(postorder[i]);
}
return true;
}