Microsoft and other data structures and algorithms interview question 9

Source: Internet
Author: User

Question 9
Judge whether the integer sequence is the result of Binary Search Tree's post-sequential Traversal
Question: enter an integer array to determine whether the array is the result of a binary search tree's sequential traversal.
If true is returned, otherwise false is returned.
For example, input 5, 7, 6, 9, 11, 10, and 8, because the integer sequence is the result of post-sequential traversal of the following tree:
8
/\
6 10
/\/\
5 7 9 11
Therefore, true is returned.
If 7, 4, 6, and 5 are input, the result of post-sequential traversal of no tree is this sequence, so false is returned.

Analysis:
This topic is to evaluate the nature of the Binary Search Tree in descending order, that is, the right subtree is greater than the root node and greater than the left subtree. Therefore, this property can be used recursively for determination.

First, the root node of the entire tree is at the end of the sequence, and then determine whether the Left and Right Decision Trees satisfy the above properties. What needs to be pointed out here is, you do not need to determine whether the right subtree is greater than the root node, because the right subtree can be further divided into left and right decision trees when being split, therefore, you can determine only the left subtree without determining the right subtree.
When the exit of the recursive program is the determined sequence length of 2 and 3, only the left subtree and root node, the right subtree of the Left subtree and the root node are available, write the determination method and return value for both cases.

Code:
[Cpp]
# Include <iostream>
 
Using namespace std;
 

Bool CheckSeqBSTree (int * sequence, int startIndex, int length)
{
If (NULL = sequence | length = 0)
Return false;
// Locate the delimiter of the left and right subtree
Int I = startIndex;
Int leftLength = 0;
Int rightLength = 0;
 
For (; I <startIndex + length-1; I ++, leftLength ++)
{
If (sequence [I]> sequence [startIndex + length-1])
Break;
}

RightLength = length-leftLength-1;
 
// Note: in fact, the right subtree does not need to be judged because each time the right subtree is split into left and right subtree, only the completeness of the Left subtree is determined.
Bool BSTree = false;
// Check whether the vertex after the check is greater

// For (; I <length-1; I ++)
//{
// If (sequence [I] <sequence [length-1])
// BSTree = false;
 
//}

// Recursive program exit
If (length = 2)
{
If (sequence [startIndex] <sequence [startIndex + 1])
BSTree = true;
}
Else if (length = 3)
{
If (sequence [startIndex] <sequence [startIndex + 2] & sequence [startIndex + 1]> sequence [startIndex + 2])
BSTree = true;
}
Else
{
BSTree = CheckSeqBSTree (sequence, startIndex, leftLength) & CheckSeqBSTree (sequence, I, rightLength );
}

 
Return BSTree;
}
Int main ()
{
 
Int a [] = {5, 7, 6, 9, 11, 10, 8 };
 
 
Cout <CheckSeqBSTree (a, 0, 7 );
 
Return 0;
}

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.