27min
1.list.pop()返回的不是数组,而是数
2.or 写成了and 导致-1数组越界
3.root写成0导致反例并不能及时终止
最终还是通过某个样例进行错误排查
class Solution:
def VerifySquenceOfBST(self, sequence:List):
if not sequence:return False
return self.dfs(sequence)
def dfs(self,sequence):
if not sequence or len(sequence)==1:return True # or写成了and
root=sequence[-1]
i=0
while i<len(sequence)-1 and sequence[i]<=root: # i的边界条件要加上
i+=1
c=i
while i<len(sequence)-1:
if sequence[i]<=root:return False # 妈的这里写成0了
i+=1
return self.dfs(sequence[:c]) and self.dfs(sequence[c:-1])
网友评论