题解 | #二叉搜索树的后序遍历序列#
二叉搜索树的后序遍历序列
http://www.nowcoder.com/practice/a861533d45854474ac791d90e447bafd
# -*- coding:utf-8 -*- def verify(seq): if len(seq)<=1: return True root=seq[-1] left=0 right=len(seq)-1 mid=right for i in range(left,right): if seq[i] <root: continue mid=i break left_q=seq[left:mid] right_q=seq[mid:right] for j in right_q: if j<=root: return False sig_l=verify(left_q) sig_r=verify(right_q) sig=sig_l & sig_r return sig # write code here class Solution: def VerifySquenceOfBST(self, sequence): if sequence==[]: return False return verify(sequence)