Wins the Offer (XXIII): subsequent binary search tree traversal sequence

Wins the Offer (XXIII): subsequent binary search tree traversal sequence

Search micro-channel public number: 'AI-ming3526' or 'this small computer vision' for more algorithms, machine learning, dry
CSDN: https://blog.csdn.net/baidu_31657889/
GitHub: https://github.com/ aimi-cn / AILearners

First, the primer

This series is my brush "to prove safety Offer" brush off the cattle in question notes online, it aims to enhance the ability under its own algorithm.
View the complete algorithm to prove safety issues resolved Offer Click: to prove safety Offer complete analytical exercises

Second, the title

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 the input array of any two numbers are different from each other.

1, ideas

First we have to understand the concept of post-order traversal. The last number is the root node traversal, the value is less than the left subtree of the root node, the value is greater than the right subtree root node.

for example:

file

In {} 5,7,6,9,11,10,8 example, the last digit of the result postorder root value is 8. In this array, the first three numbers 6, 7 and 8 smaller than, the value of the node 8 is a left subtree node; 3 digits than 8 9, 11 and 10 large, the value is right subtree node node 8.

We next determined the structure of the sub-tree corresponding to each part of the array the same way. In fact, this is a recursive process. For sequence 5,7,6, 6 the last number is the value of the left subtree of the root node. Figures 5 to 6 hours, 6 is a value of the node a left child node, while 7 is a right child node. Similarly, in the sequence 9,11,10, the last numeral 10 is a right subtree of the root node, small figures 9 to 10, the node 10 is the value of the left child node, while 11 is a right child node.

We use the recursive method, first determine the position of the left subtree and right subtree of the array, and then determine the left subtree, right subtree is not a binary search tree.

2, programming

python

Code implementation:

# -*- coding:utf-8 -*-
class Solution:
    def VerifySquenceOfBST(self, sequence):
        # write code here
        if not len(sequence):
            return False
        if len(sequence) == 1:
            return True
        # 定义整个数的长度
        length = len(sequence)
        # 根节点的值
        root = sequence[-1]
        i = 0
        # 判断左子树的位置
        while sequence[i] < root:
            i = i+1
        k = i
        # 判断右子树的值是否都大于根节点root
        for j in range(i,length-1):
            if sequence[j] < root:
                return False
        left_s = sequence[:k]
        right_s = sequence[k:length-1]
        left , right = True, True
        # 递归遍历左右子树的值
        if len(left_s) > 0:
            left = self.VerifySquenceOfBST(left_s)
        if len(right_s) > 0:
            right = self.VerifySquenceOfBST(right_s)
        return left and right

AIMI-CN AI learning exchange group [1015286623] for more information on AI

Sharing technology, fun in life: our number of public computer vision this small push "AI" series News articles per week, welcome your interest!

Guess you like

Origin www.cnblogs.com/aimi-cn/p/11428376.html