剑指offer面试题33. 二叉搜索树的后序遍历序列(二叉树)(递归)

题目描述

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 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)
发布了195 篇原创文章 · 获赞 566 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_37763870/article/details/105281411