After offer_ prove safety _ binary search tree binary sequence preorder

Subsequent binary search tree traversal sequence

Here Insert Picture Description
Answers
questions resolved
idea: the nature of the binary search tree recursive array to determine whether compliance with the nature, in line with the output yes, do not meet the no output

Binary search tree is a binary search ordered array search tree is formed, which refers to an empty tree or a binary tree having the following properties:

If the left subtree of any node is not empty, then the value of the left sub-tree, all the nodes are less than the value of the root node;
if any of the right subtree of nodes is not empty, the right subtree are greater than values of all the nodes it the value of the root node;
left any node, respectively for the right subtree binary search tree.

# -*- coding:utf-8 -*-
class Solution:
    def VerifySquenceOfBST(self, sequence):
        # write code here
        if len(sequence)==0:
            return 0
        index = 0
        for i in range(len(sequence)):
            if sequence[i] > sequence[-1]:
                index = i
                break
        for j in range(i, len(sequence)-1):
            if sequence[j] < sequence[-1]:
                return False
        left = True
        right = True
        if len(sequence[:index])>0:
            left = self.VerifySquenceOfBST(sequence[:index])
        if len(sequence[index:-1])>0:
            right = self.VerifySquenceOfBST(sequence[index:-1])
        return left and right
Published 31 original articles · won praise 0 · Views 724

Guess you like

Origin blog.csdn.net/freedomUSTB/article/details/105105507