Java - post-order traversal sequence of binary search tree

topic link

Niuke online oj question - post-order traversal sequence of binary search tree

topic description

Input an integer array and judge whether the array is the result of postorder traversal of a binary search tree. Returns true if yes, false otherwise. Assume that any two numbers in the input array are different from each other.

Data range: the number of nodes is 0≤n≤1000,
the value on the node satisfies 1≤val≤10^5, and the value on the node is guaranteed to be different
Requirements: space complexity O(n), time time complexity O(n^ 2 )

hint:

  1. A binary search tree is a tree in which the parent node is larger than all the nodes in the left subtree but smaller than all the nodes in the right subtree.
  2. In this question, we agree that an empty tree is not a binary search tree
  3. Post-order traversal refers to traversing in the order of "left subtree-right subtree-root node"
  4. Refer to the following binary search tree, example 1
    insert image description here

Topic example

Example 1

Input:
[1,3,2]

return value:
true

Description:
It is the post-order traversal of the above picture, and returns true

Example 2

Input:
[3,1,2]

return value:
false

Explanation:
It does not belong to the post-order traversal of the above figure, and the sequence cannot be traversed from another binary search tree, because the last 2 must be the root node, and the front must be the child node, which may be the left child or the right child. The root node may also be the full left child, the root node, or it may be the full right child, the root node, but the combination of [3,1,2] cannot satisfy these conditions, so return false

Example 3

Input:
[5,7,6,9,11,10,8]

return value:
true

problem solving ideas

The characteristic of the binary search tree is that the parent node is greater than the value of all nodes in the left subtree and smaller than the value of all nodes in the right subtree

The post-order traversal is to traverse the left subtree first, then traverse the right subtree, and then output the value of the parent node

The subscript of the first element of the definition array is start, and the subscript of the last element is end.
First get the value corresponding to the end subscript of the last node in the array. This value is the value of the parent node, and then traverse the array from left to right. When an element with a greater value than the parent node is encountered, the node subscript For i,
it can be determined that [start, i - 1] is the left subtree sequence of the current traversal sequence, and [i, end - 1] is the right subtree sequence of the current traversal sequence

Continue to traverse the [i, end - 1] sequence, if any element is smaller than the value of the parent node, it means that this sequence does not satisfy the nature of the binary search tree, return false

It should be noted that currently it is only determined whether the current sequence satisfies the nature of the binary search tree, and we need to judge whether all subtrees satisfy the nature of the binary search tree, that is to say, we have to perform the above process respectively [start, i - 1] sequence and [i, end - 1] sequence are verified again

Therefore, it can be verified by recursion, and the final termination condition is: when start <= end, return true

full code

public class Solution {
    
    
    public boolean VerifySquenceOfBST(int [] sequence) {
    
    
        if(sequence == null || sequence.length == 0){
    
    
            return false;
        }

        int start = 0;
        int end = sequence.length - 1;
        return VerifySquenceOfBSTHelper(sequence, start, end);
    }

    private boolean VerifySquenceOfBSTHelper(int[] sequence, int start, int end) {
    
    
        if(start >= end){
    
    
            return true;
        }
        
        int root = sequence[end];
        int i = start;
        while(i < end && sequence[i] < root){
    
    
            i++;
        }
        for(int j = i; j < end; j++){
    
    
            if(sequence[j] < root){
    
    
                return false;
            }
        }
        return VerifySquenceOfBSTHelper(sequence, start, i - 1) && VerifySquenceOfBSTHelper(sequence, i, end - 1);
    }
}

Guess you like

Origin blog.csdn.net/m0_60867520/article/details/130346450