A judgment is not array after traversing a binary search tree results

--- --- restore content begins

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.

Determine whether an array is a binary search tree traversal sequence of results we must first know the binary search tree traversal sequence is kind of how.

Binary search tree features: other nodes except leaf nodes, the left subtree of a node in any node that is not greater than a number, any number of a right subtree is not less than this node.

Postorder binary tree: For a sequence S, is the last element of x (i.e. the root), if the sequence is to remove the last element T, then T satisfies: T can be divided into two sections, the front section (left subtree) of less than x, after a period (right subtree) is greater than x, and these two (subtree) of the subsequent sequences are legitimate.

 Analyzing T is not all recursive postorder binary tree.

public class Solution {
    public boolean VerifySquenceOfBST(int[] sequence) {
        if (sequence == null || sequence.length == 0) {
            return false;
        }
        return verifySquenceOfBST(sequence, 0, sequence.length - 1);
    }
 
    boolean verifySquenceOfBST(int[] sequence, int start, int end) {
        if (start == end) {
            return true;
        }
        int= Key Sequence [End];
         int I = Start;
         for (; I <End; I ++ ) {
             IF (Sequence [I]> Key) { // find the first element is larger than the last element, the confirmation of T left subtree and right subtree 
                BREAK ; 
            } 
        } 
        int m = I;
         for (; I <End; I ++ ) {
             IF (Sequence [I] <Key) { // if whether a right subtree is smaller than the last element element returns to false 
                return  to false ; 
            } 
        } 
        IF (m || m == == Start End) { //All the elements than the last element of a small case that this sequence only the left sub-tree T, then continue to determine its left subtree. The first element is larger than the last element, similarly,
 // continue to determine its right subtree 
            return verifySquenceOfBST (Sequence, Start, End -. 1 ); 
        } the else {
             return verifySquenceOfBST (Sequence, Start, m -. 1) && verifySquenceOfBST (Sequence, m, End -. 1 ); 
        } 
    } 
    
}

 

Guess you like

Origin www.cnblogs.com/smilepup-hhr/p/11297538.html