classSolution:defverifyPreorder(self,preorder: List[int]) ->bool:ifnot preorder:returnTrue root = preorder[0] left = [] right = [] i =1while i <len(preorder):if preorder[i]< root: left.append(preorder[i])else:break i +=1while i <len(preorder):if preorder[i]> root: right.append(preorder[i])else:returnFalse i +=1return self.verifyPreorder(left)and self.verifyPreorder(right)
classSolution:defverifyPreorder(self,preorder: List[int]) ->bool: defhelper(left,right):if left >= right:returnTrue root = preorder[left] i = left +1while i <= right and preorder[i]< root: i+=1 j = iwhile j <= right:if preorder[j]<= root:returnFalse j +=1returnhelper(left+1, i-1)andhelper(i, right)returnhelper(0, len(preorder) -1)