题解 | #二叉搜索树的后序遍历序列#

二叉搜索树的后序遍历序列

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)
        
        
        

全部评论

相关推荐

joe2333:怀念以前大家拿华为当保底的日子
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务