Follow-up traversal of binary search tree

Source: Internet
Author: User

Given an array of integers, is it possible to determine whether this array is the result of a subsequent traversal of a binary search tree?

Because of the nature of the binary search tree, any node in its left subtree is smaller than the node value, the node in the right subtree is larger than the node value, then after the subsequent traversal of the array should have an attribute, that is, the previous element of the array is smaller than the last element value, The element values later are larger than the last element value. And this part of the previous element and the following elements also conform to the same law, according to this feature, can be solved by recursion.

Such as:


The code is as follows:

#include "util.h"/* follow-up traversal of the two-fork search tree * Enter an array to determine whether the elements in the array are the result of a subsequent traversal of a binary search number *//* because of the characteristics of the binary tree, the nodes in the left subtree of any node are smaller than the node value. The node values in the right subtree are larger than the current value * and since the subsequent traversal of the root node is the last access, it is possible to find out which nodes in the array are the nodes of its left subtree based on the last value, * which nodes are nodes of its right subtree *///if it can form a subsequent traversal of a two-fork tree, returns True, Otherwise return falsebool binarysearchtreepost (int *datas,int begin,int end) {//If input data is 1 effective, return to Falseif (datas==null| | Begin>end) return false;//if begin and end are equal, representing only one node, one node can be considered a two-fork search tree if (begin==end) return True;int next=begin;// Left this subscript is used to record the index of the last node of Zuozi int left=begin;for (; next<end;next++) {///Find the first node greater than Datas[end], then all nodes before this node should be the nodes in the subtree. This node and the node after it are nodes in the right subtree if (Datas[next]>datas[end]) {break;}} left=next-1;//the nodes in the right subtree should be greater than this datas[end] if the requirements are not met, return FalseFor (; next<end;next++) {if (Datas[next]<datas[end]) return false;} BOOL leftflag,rightflag;leftflag=rightflag=true;//only need to continue traversing if (Left>=begin) if record has at least one element leftflag= Binarysearchtreepost (Datas,begin,left)///If you need to traverse the IF (left<end-1) rightflag=binarysearchtreepost only if there is at least one element in the right subtree ( DATAS,LEFT+1,END-1); return leftflag&&rightflag;} int main () {int num[]={5,7,6,9,11,10,8};bool flag=binarysearchtreepost (num,0,sizeof (num)/sizeof (num[0])-1); const char * output=flag? " True ":" false ";std::cout<<" binary search tree post-post traversal: "<<std::endl;printarrayt (num,sizeof (num)/sizeof (num[0])); STD: : Cout<<output <<std::endl;int num1[]={1,2,3,4,5,6};flag=binarysearchtreepost (num1,0,sizeof (NUM1)/ sizeof (Num[0])-1); Output=flag? " True ":" false ";p Rintarrayt (num1,sizeof (NUM1)/sizeof (num1[0])); Std::cout<<output<<std::endl;return 0;}


Follow-up traversal of binary search tree

Related Article

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.