Enter an integer array to determine whether the array is the result of a post-order traversal of a binary search tree. If it is, it returns true, otherwise it returns false. Assume that any two numbers in the input array are different from each other.

Title description:
Enter an integer array to determine whether the array is the result of a post-order traversal of a binary search tree.
If it is, it returns true, otherwise it returns false. Assume that any two numbers in the input array are different from each other.
(Ps: We agree that the empty tree is not a binary search tree)
Example 1
input

[4,8,6,12,16,14,10]
return value

true

Thinking analysis:
The characteristic of binary search tree is: if the current node has a left subtree, then the value of each node of the left subtree is less than or equal to the value of the current node; if the current node has a right subtree, then the value of the right subtree The value of each point is greater than the value of the current node;
let's analyze again: What are the characteristics of the post-order traversal of the tree? The traversal order must be the left subtree first, then the right subtree, and finally the root node.
According to these two characteristics, we can know that in the incoming subsequent traversal sequence, the last value is the root node. In addition to the root node, the sequence is divided into two parts:

Take the first value greater than the root node as the dividing line. The sequence after the first value greater than the root node is the right subtree of the root node;
judged by the values ​​of these right subtrees:

  • Each one is greater than the root node,
    if it is greater than it means that the current node satisfies the characteristics of a balanced binary tree. Next, we do this for the left subtree and the right subtree respectively.
  • If one is not satisfied,
    it is not a subsequent traversal that satisfies the condition

Repeat the two operations until no nodes can be traversed.

Code display:


/*
*输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。
* 如果是则返回true,否则返回false。假设输入的数组的任意两个数字都互不相同。
* (ps:我们约定空树不是二叉搜素树)
* */
public class Solution {
    
    

    public boolean VerifySquenceOfBST(int [] sequence) {
    
    
        //二叉搜索树的特点,每个根节点都满足:节点的左子树都小于等于节点的值;节点的右子树都大于根节点的值
        //int index=sequence.length-1;
        if(sequence.length == 0){
    
    
            return false;
        }
        return traverse(sequence,0,sequence.length-1);

    }
    public boolean traverse(int[] sequence,int start,int end){
    
    
        if(start>=end){
    
    
            return true;
        }
        int key=sequence[end];
        int i=0;
        for( i=start;i<end;i++){
    
    
            if(sequence[i]>key)
                break;
        }
        for(int j=i;j<end;j++){
    
    
            if(sequence[j]<key){
    
    
                return false;
            }
        }
        return traverse(sequence,start,i-1)&&traverse(sequence,i,end-1);//在子树中遍历,除去根节点
    }

}

Guess you like

Origin blog.csdn.net/weixin_43815275/article/details/114888933