Python subsequent binary search tree traversal sequence

Enter an integer array, the array is not the result of the determination after traversing a binary search tree. If the output Yes, otherwise the output No. Suppose any two digital input array are different from each other hungry.

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def VerifySequenceOfBST(self, sequence):
        if sequence==[]:
            return False
        rootNum=sequence[-1]
        del sequence[-1]
        index=None
        for i in range(len(sequence)):
            if index == None and sequence[i]>rootNum:
                index=i
            if index!=None and sequence[i]<rootNum:
                return False
        if sequence[:index] ==[]:
            leftRet = True
        else:
            leftRet=self.VerifySequenceOfBST(sequence[:index])
        if sequence[index:]==[]:
            rightRet=True
        else:
            rightRet = self.VerifySequenceOfBST(sequence[index:])
        return leftRet and rightRet
Published 135 original articles · won praise 121 · Views 4860

Guess you like

Origin blog.csdn.net/weixin_44208324/article/details/105314452