Sequential traversal sequence of binary search tree

Source: Internet
Author: User

Topic

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

Analysis

Suppose the input array {5,7,6,9,11,10,8} to determine whether it is a binary search tree after the sequential traversal results, it is necessary to combine the characteristics of binary search tree and the method of post-order traversal to judge, the post-order traversal feature is the last binary tree root node, so it is determined that root nodes are 8, and first traverse Zuozi , and then traverse the right subtree, so the key is to determine the Zuozi right sub-tree demarcation point where. By the two-fork search tree Suzi node, the right subtree is greater than the characteristics of the root node can be found in the demarcation point, {5,7,6} are small root node 8, so it is a left dial hand tree, and {9,11,10} is greater than the root node, so for the right subtree, the left dial hand tree, right subtree respectively using the above method of judgment can be, Once the small root node appears in the right subtree, the array is not the result of a sequential traversal of a binary search tree.

To cite an example: {7,4,6,5}
1. root node is 5;
2. The first element of the array is not less than 5, the description tree has no left subtree, only the right subtree;
3. For {7,4,6} are all right subtree nodes, it should all be greater than 5, but the existence of 4 can be determined directly, this array is not post-order traversal results.

"Test Code"

#include <stdio.h>#include <stdlib.h>#define FALSE 0#define TRUE 1BOOLVerifysequenceofbst (intSequence[],intLength) {if(sequence = = NULL | | length <=0)return false;intRoot = sequence[Length-1];//Two forks the node of the left sub-tree in the search tree is small root node .    inti =0; for(; i < length-1; ++i) {if(Sequence[i] > root) Break; }//In binary search tree, the node of right subtree is greater than the root node .    intj = i; for(; J < Length-1; ++J) {if(Sequence[j] < root)return false; }//Determine if the left subtree is a binary search tree    BOOLleft =true;if(I >0) left = Verifysequenceofbst (sequence, i);//Judging right subtree is not binary search book    BOOLright =true;if(I < length-1) right = VERIFYSEQUENCEOFBST (sequence + i, length-i-1);returnLeft & right;}voidMain () {intS[] = {5,7,6,9, One,Ten,8};if(Verifysequenceofbst (s),7))printf("There is a binary search book whose post-order traversal result is {5,7,6,9,11,10,8}\n");Else        printf("There is no binary search book, its post-order traversal result is {5,7,6,9,11,10,8}\n");intS2[] = {7,4,6,5};if(Verifysequenceofbst (S2,7))printf("There is a binary search book whose post-order traversal result is {7,4,6,5}\n");Else        printf("There is no binary search book, its post-order traversal result is {7,4,6,5}\n");}

Output

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.