Prove safety offer: subsequent binary search tree traversal sequence (java)

Title: Enter an integer array, the array is determined not after a binary search tree in preorder traversal results. If it returns true, otherwise false. Suppose the input array of any two numbers are different from each other.

    } {E.g. 5,7,6,9,11,10,8 input array Returns true, if the input is an array {7,4,6,5}, because there is no result of which pieces of the subsequent binary search tree traversal this sequence is, therefore, false.

   Sequence preorder obtained, the last number is the value of the root node of the tree. Preceding numbers in an array can be divided into two parts: The first part is the value of the left subtree of the node, the root node than their small value; the second part is the value of the right subtree of nodes, the root node than the value of their Big.

    {} 5,7,6,9,11,10,8 an array, for example, the last digit of the result postorder 8 is the value of the root node. 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.

    Let us analyze another array {7,4,6,5}. Postorder last tree is the root node, so the value of the root node 5. Since the first number is greater than 7 5, thus the corresponding binary search tree, no left subtree root node, 7 figures , value 4, and 6 are right subtree node. However, we have found the value of a node is 4, 5 is smaller than the value of the root node in the right subtree, contrary to the definition of the binary search tree. So there is no a binary search tree, its post-order traversal of the result is 7,4,6,5.

 public boolean verifySequenceOfBST(int[] array,int start,int end) throws Exception{  
        if(array == null||array.length<=0)  
            return false;  
       if(start < 0){  
            throw  new Exception("first can't be less than 0");  
        }  
        if(end > array.length){  
            throw new Exception("last can't be greater than the count of the element.");  
        }  
        int root = array[end];  
        //在二叉搜索树中左子树的结点小于根节点  
        int i = start;  
        for(; i < end;i++){  
            if(array[i]>root)  
                break;  
        }  
        //在二叉搜索树中右子树的结点大于根节点  
        int j = i;  
        for(;j < end;j++){  
            if(array[j] < root)  
                return false;  
        }  
        //判断左子树是不是二叉搜索树  
        boolean left = true;  
        if(i >start)  
            left =verifySequenceOfBST(array ,start,i-1);  
        //判断右子树是不是二叉搜索树  
        boolean right = true;  
        if(i < end)  
            right =verifySequenceOfBST(array,i,end-1);  
        return (left && right);  
    }  


发布了118 篇原创文章 · 获赞 35 · 访问量 12万+

Guess you like

Origin blog.csdn.net/abc7845129630/article/details/52729262