Niuke.com's sword refers to Offer - post-order traversal sequence of binary search tree

Topic description

Input an integer array to determine whether the array is the result of a post-order traversal of a binary search tree. If yes, output Yes, otherwise output No. Suppose any two numbers of the input array are different from each other.

concept

1. Binary search tree:

Binary Search Tree (English: Binary Search Tree), also known as binary search tree, ordered binary tree (English: ordered binary tree), sorted binary tree (English: sorted binary tree), refers to an empty tree or has the following properties The binary tree of :

  • Any node, if the left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node;
  • For any node, if the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
  • The left and right subtrees of any node are also binary search trees respectively;
  • There are no nodes with equal keys.

2. Post-order traversal: Post-order traversal first traverses the left subtree, then traverses the right subtree, and finally visits the root node. When traversing the left and right subtrees, it still traverses the left subtree first, then the right subtree, and finally Traverse the root node.

method one

1. Since it is a post-order traversal, the traversal order is the left and right roots, so the last node of the sequence must be the root node;

2. According to the characteristics of binary search tree: the left subtree is smaller than the root node, and the right subtree is larger than the root node. It is easy to find the left subtree and right subtree of the root node according to the comparison of the value of the element and the root node. Then determine whether the value of the left subtree node is less than the value of the root node, whether the value of the right subtree node is greater than the value of the root node, and determine whether the sequence corresponding to the left and right subtrees is the result of the post-order traversal of the binary search tree. Obviously, the problem can be solved with recursive thinking.

code

class Solution {
public:
    bool VerifySquenceOfBST(vector<int> sequence) {
        if( sequence.empty() )
            return false;
        return help( sequence, 0, sequence.size()-1 );
    }
    bool help( vector<int> input, int first, int last )
    {
        if( last-first <= 1 )
            return true;
        int small_last = last-1;
        while( input[small_last] > input[last] )//Find the dividing point of left and right subtrees
            small_last--;
        for( int i=first;i<=small_last;i++ )//guarantee that the left subtree part is less than root
            if( input[i] > input[last] )
                return false;
        return help(input, first, small_last) && help(input, small_last+1, last-1 );
    }
};

Method Two

Due to the characteristics of post-order traversal and binary search tree, for each node in the sequence, the nodes located before the node, viewed from left to right, should be consecutively less than the value of the node, and then consecutively greater than the node. If there is an intersection phenomenon, it means that the sequence is not the result of the post-order traversal of the binary search tree. The description is not very clear, just look at the code directly.

code

class Solution {
public:
    bool VerifySquenceOfBST(vector<int> sequence)
    {
        int length=sequence.size();
        if(length==0)
            return false;
        int i=0;
        while(--length)
        {
            while(sequence[i++]<sequence[length]);
            while(sequence[i++]>sequence[length]);
            if(i<length)
                return false;
            i=0;
        }
        return true;
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325764807&siteId=291194637