Determines whether the array sequence is a sequential traversal of a two-fork search tree

Source: Internet
Author: User

#include <iostream>using namespacestd;BOOLIspostorderofbst (intPostorder[],intLowintHigh ) {    if(Postorder = = NULL | | Low <0|| High <0)return false; if(Low = = high)return true; intPivot = high-1;//find the dividing point of the Zuozi and right sub-tree     while(Pivot >=0&& Postorder[pivot] >Postorder[high]) {        --pivot; }    intLowindex =pivot;  while(Lowindex >=0&& Postorder[lowindex] <Postorder[high]) {        --Lowindex; }    if(Lowindex >=0)return false;//left dial hand tree all elements are smaller than the root node    if(Pivot +1< high &&!ispostorderofbst (postorder, pivot +1, High-1))//Judging right sub-tree    {        return false; }    if(Low <= pivot &&!ispostorderofbst (postorder, Low, pivot))//Judging the left sub-tree    {        return false; }    return true;}intMainintargcChar Const*argv[]) {    intPostorder0[] = {5,7,6,9, One,Ten,8}; intPostorder1[] = { One,Ten,9,8,7,6,5}; intPostorder2[] = {5,6,7,8,9,Ten, One}; intPostorder3[] = {8,9,5, One,Ten,7,6}; intPostorder4[] = {7,6, A,5, One,Ten,9}; cout<<"{5, 7, 6, 9, one, ten, 8}---"<< Ispostorderofbst (Postorder0,0,6) <<Endl; cout<<"{One, ten, 9, 8, 7, 6, 5}---"<< Ispostorderofbst (Postorder1,0,6) <<Endl; cout<<"{5, 6, 7, 8, 9, ten, one}---"<< Ispostorderofbst (Postorder2,0,6) <<Endl; cout<<"{8, 9, 5, one, ten, 7, 6}---"<< Ispostorderofbst (Postorder3,0,6) <<Endl; cout<<"{7, 6, A, 5, one, ten, 9}---"<< Ispostorderofbst (Postorder4,0,6) <<Endl; return 0;}

Determines whether the array sequence is a sequential traversal of a two-fork 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.