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