# Determines whether an array is a two-fork search tree after a sequence of traversal sequences 24

Source: Internet
Author: User

Introduction

??

Continue the binary tree, the knowledge point of this question is the follow-up traversal of the two-fork tree

??

Analyze problems

??

For a binary tree sequential traversal sequence, the last number must be the root node, and then the previous number, from the beginning to the first is greater than the number of root node is the number of left subtree, and the second to the penultimate number should be greater than the root node, is the right subtree, if the following number has a small root node, So this sequence is not a sequential traversal of a binary search tree.

??

Solve the problem

??

The same is done with recursion, first consider the Corner case, if the array is empty, then return false

??

Then find the root node, the root node is the last number of array, starting from 0 to find the first number greater than root

??

So the value from 0 to-1 is the number in the left dial hand tree.

??

Then from i to array. LENGTH-2 traversal, if there are fewer than root, then this sequence is definitely not a sequential traversal sequence

If not, then I to array. Length-2 is the number in the right sub-tree.

??

Then see if the sub-sequences in the left subtree and those in the right subtree meet the criteria

??

Note that there is a need to use the copy function of the array, otherwise there will be problems

??

public boolean Verifysequenceofbst (int[] array)

{

if (Array==null | | array.length<=0)

return false;

int root=array[array.length-1];

int i=0;

for (; i<array.length-1;++i)

{

if (array[i]>root)

{

Break

}

}

int j=i;

for (; J < array.length-1; ++j) {

if (array[j]<root) {

return false;

}

}

Boolean leftflag=true;

if (i>0) {

Leftflag=verifysequenceofbst (Arrays.copyofrange (array,0,i));

}

Boolean rightflag=true;

if (i<array.length-1) {

Rightflag=verifysequenceofbst (Arrays.copyofrange (array,i,array.length-1));

}

Return leftflag&&rightflag;

Determines whether an array is a two-fork search tree after a sequence of traversal sequences 24

Related Keywords:

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.