题解 | #树的子结构#
二叉搜索树的后序遍历序列
http://www.nowcoder.com/practice/a861533d45854474ac791d90e447bafd
代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
@param sequence int整型一维数组
@return bool布尔型
class Solution:
def VerifySquenceOfBST(self , sequence: List[int]) -> bool:
# write code here
if sequence==[]:
return False
index=None
Number=sequence[-1]
del sequence[-1]
for i in range(len(sequence)):
if index==None and sequence[i]>Number:
index = i
if index!=None and sequence[i]<Number:
return False
if sequence[:index] == []:
left = True
else:
left=self.VerifySquenceOfBST(sequence[:index])
if sequence[index:-1] == []:
right = True
else:
right=self.VerifySquenceOfBST(sequence[index:])
return left and right