剑指 Offer 33. 二叉搜索树的后序遍历序列
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。
class Solution: def verifyPostorder(self, postorder: List[int]) -> bool: def recur(i, j): if i >= j : return True p = i while postorder[p] < postorder[j] : p = p + 1 m = p while postorder[p] > postorder[j] : p = p + 1 return p == j and recur(i , m -1) and recur(m , j- 1) return recur(0, len(postorder) - 1)