Verification of sequential traversal sequence of binary search tree

Source: Internet
Author: User

"title" Enter an array of integers to determine if the array is the result of a sequential traversal of a binary search tree. If yes, returns true, otherwise the output is false. Assume that any two digits of the input array are different.

"Idea" first we need to know what is a two-fork search tree, a two-fork lookup tree (binary search trees), (also: two-tree, binary-sort tree) It is either an empty tree, or a two-prong tree with the following properties: If its left subtree is not empty, The value of all nodes on the left subtree is less than the value of its root node, and if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node, and its left and right subtrees are also two-fork sorting trees. by this nature, we can know that the element on the left side of the root node must be smaller than the root node, the element on the right side of the root node is always larger than the root node, and the subtree also conforms to this law, because it is a post-order traversal, so the last element of the integer array is the root node, it is smaller than the Bigger than it is the right sub-tree element, and the left and the sub-tree are consistent with this law. So we get the last element of an array of integers as the root node, and then iterate through all the elements in front of it, and find the first one larger than it, then this element is delimited, the front is the left subtree element, followed by the right sub-tree element, if all elements behind this element are larger than the root node, it is correct, and vice , and we continue to judge by the left and right of the subtree

public class Solution {public boolean verifysquenceofbst (int [] sequence) {return check (sequence,0,sequence.le    NGTH-1);            The public boolean check (int []array,int begin,int end) {//If there is only one element, recursively deepest, returns true if (Begin >= End)        return true;        int root = Array[end];        int Firstmax =-1;                for (int index = Begin;index < End;index + +) {if (Array[index] > root) {firstmax = index;            Break                }}//If there is no larger than the root node, indicating there is no right subtree, then directly determine the left subtree if (Firstmax = =-1) return check (ARRAY,BEGIN,END-1);             If there is a right subtree, then check that the right subtree is all greater than the root node for (int i = Firstmax+1;i < end;i++) {//If the right subtree exists smaller than the root node, returns false        if (Array[i] < root) return false;    }//recursive check left and right subtree return (check (array,begin,firstmax-1) && check (array,firstmax,end-1)); }}

Verification of sequential traversal sequence of binary search tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.