题目描述
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。
思路
详见链接
代码
class Solution:def verifyPostorder(self,postorder:[int])->bool:def recur(i,j):if i >= j:return Truel = iwhile postorder[l] < postorder[j]:l += 1m = lwhile postorder[l] > postorder[j]:l += 1return l == j and recur(i,m-1) and recur(m,j-1)return recur(0, len(postorder)-1)
如果觉得《剑指offer面试题33. 二叉搜索树的后序遍历序列(二叉树)(递归)》对你有帮助,请点赞、收藏,并留下你的观点哦!