After the binary search tree in preorder Analyzing

Title Description

Enter an integer array, the array is not the result of the determination after traversing a binary search tree. If the output Yes, otherwise the output No. Suppose the input array of any two numbers are different from each other.

Before the results after the binary search tree, just know that in order traversal is increasing, today do This question, asked to judge the array is not a binary search tree traversing, a start bit ignorant, and later painting on paper, feeling like the feeling of recursion, there is a special kind of feeling like merge sort of the same, and then found for each of the sub-structure of a tree, we will be divided into two parts, left and right sub-tree can quite understand this recursive structure of the , divided into two parts, right and left, respectively, and recursive, until the leaf node

 1 public class Solution {
 2     public boolean VerifySquenceOfBST(int [] sequence) {
 3         if(sequence.length==0)
 4             return false;
 5           return helper(sequence,0,sequence.length-1);
 6     }
 7     public boolean helper(int[] arr, int l,int r)
 8     {
 9         if(l>=r)
10             return true;
11         for(int i=l;i<r;i++)
12         {
13             if(arr[i]>arr[r])
14             {
15                 for(int j=i;j<r;j++)
16                     if(arr[j]<arr[r])
17                         return false;
18                 return helper(arr,l,i-1)&&helper(arr,i,r-1);
19             }
20         }
21         return true;
22     }
23 }

 

Guess you like

Origin www.cnblogs.com/cold-windy/p/11544531.html