题目描述:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。
分析:
中序判断法,因为二叉搜索树满足左子树 < 根节点 < 右子树,其中序遍历是有序的,而且中序、后序分别对应栈的压入、弹出序列(首先左子树入栈,左子树弹出,然后根节点、右子树入栈,右子树、根节点弹出),因此可以通过将当前后序遍历排序, 然后判断中序,后序是否满足栈的压入、弹出即可。
求解:
// 中序判断法
function verifyPostorder(postorder: number[]): boolean {
const inorder = [...postorder];
inorder.sort((a, b) => a - b);
return validateStackSequences(inorder, postorder);
}
// 辅助栈模拟法
export function validateStackSequences(pushed: number[], popped: number[]): boolean {
const stack: number[] = [];
const size = pushed.length;
// 弹出序列当前应该弹出元素的下标
let poppedIndex = 0;
// 出栈和入栈序列不全为空
for (let i = 0; i < size; i++) {
const element = pushed[i];
// 将入栈序列元素入栈
stack.push(element);
while (stack.length && stack[stack.length - 1] === popped[poppedIndex]) {
// 栈不为空 且 栈顶元素等于弹出序列当前应弹出元素
stack.pop();
// 下一个应该弹出元素的下标
poppedIndex = poppedIndex + 1;
}
}
return stack.length === 0; // 弹出与压入对应说明满足
}