C Language Hardening (eight) judgment sequence is not the result of sequential traversal of binary search tree

Source: Internet
Author: User

In front of a series of questions about the intersection of linked lists, this blog post, let us through a simple judgement, review the array and the two-fork tree.


With this problem, you can master

    • The law of Binary search tree
    • Characteristics of post-sequential traversal
    • Use of recursion
Topicsenter an array of integers to determine if the array is the result of a sequential traversal of a two-tuple lookup tree.
Returns False if True is returned.

For example, input 5, 7, 6, 9, 11, 10, 8, because this integer sequence is the following post-order traversal result of the tree:

Therefore, returns True.
If you enter 7, 4, 6, 5, no tree has the result of a post-order traversal that is the sequence, and therefore returns false.


See two dollar find tree (or two fork find tree), first reaction, The number larger than the root node is on the right, and the number on the left is smaller than the root knot .Then we see the post-search, the first reaction, the last number to traverse is the root node.To do this two points, the problem is no difficulty, as a programming addiction. Of course, in some of the more difficult topics, it is not enough to find these two laws alone.
IdeasBecause it is a subsequent traversal, the last number traversed is the root node.
And because it is a two-fork search tree, so the number of Biggenden nodes is on the right, and the number on the left is smaller than the root node.
The number on the left is traversed more quickly than the right!
So must meet The maximum angular mark of the smallest Bibigen node in a number larger than the root node .
To the left and right sub-arrays also use the above ideas for recursive judgment!
If the length of the array is less than or equal to 3, you do not need to judge, definitely meet

Source Code
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <sstream> #include < Vector>using namespace std;/** title: Enter an array of integers to determine if the array is the result of a sequential traversal of a two-tuple lookup tree. Returns False if True is returned. The last number to traverse is the root node, if it is a subsequent traversal. And because it is a two-fork search tree, so the Biggenden node large number on the right side, the number is smaller than the root node on the left, the number of the left of the traversal than the right side of the faster is traversed! So we must satisfy the maximum angular mark of the Bibigen node with a small number of the smallest angle in the large number of the root node. Sub-arrays also use the above ideas for recursive judgment! If the length of the subarray is less than or equal to 3, then it is not necessary to determine whether the *//**IFBTREEHOUXUVT is satisfied with a vector array to determine if it is a sequential traversal result */bool Ifbtreehouxu (vector<int> vt) {if ( Vt.size () <=0| | null== &vt) return false;if (Vt.size () >3) {//If the length of the sub-array is less than or equal to 3, then it is not necessary to determine, definitely satisfy int root = Vt[vt.size () -1];//Get root node value bool Flag = True;vt.pop_back ();//reject the root node vector<int> vt_small;//The array vector<int> vt_big;//is greater than the root node of the array//traversal for ( int i = 0;i<vt.size (); i++) {//if traversing to a number is larger than the root node, the flag is set to false//below if you traverse to a number smaller than the root node, the array is not a post-order traversal result if (flag&&vt[i ]>root) Flag=false;else if (flag&&vt[i]<root) Vt_small.push_back (Vt[i]); else if (!flag&&vt[i] >root) Vt_big.push_back (Vt[i]) Elsereturn false;//This array is not a sequential traversal result}return Ifbtreehouxu (vt_smAll) &&ifbtreehouxu (VT_BIG);} return true;} Vector<int> vt_test;void initture () {vt_test.push_back (5); Vt_test.push_back (7); Vt_test.push_back (6); vt_ Test.push_back (9); Vt_test.push_back (one), Vt_test.push_back (ten); Vt_test.push_back (8);} void Initfalse () {vt_test.push_back (7); Vt_test.push_back (4); Vt_test.push_back (6); Vt_test.push_back (5);} void Main () {initture ();//initfalse (); if (Ifbtreehouxu (vt_test)) cout<< "is a sequential traversal result" <<endl;elsecout< < "not" is the sequential traversal result "<<endl;system (" pause ");}

Run diagram

Summary:two USD to find the tree, the first reaction, the number larger than the root node on the right, the number smaller than the root node on the leftPost-order lookup, first reaction, the last number to traverse is the root node.

C Language Hardening (eight) judgment sequence is not the result of sequential traversal 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.