Welcome reprint, Reproduced Please be sure to indicate the source: http://blog.csdn.net/alading2009/article/details/44872143
Question 9th: 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.
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.
Because of the characteristics of the binary search tree (the value of the left subtree is less than the root node, the value of the right subtree is greater than the root node), a binary lookup tree can be found in combination with the order sequence. A bit like a quick sort, the subtree is recursively judged.
Code
Packagetest009;/** * Created by CQ on 2015/4/3. * Question 9th: Enter an array of integers to determine if the two-fork tree is the result of a sequential traversal of a two-dollar lookup tree. If true, returns false otherwise. */ Public class Test009 { Public Static Boolean Existcorrespondbstree(int[] arr) {if(arr = =NULL){return false; }returnChildrecursion (arr,0, arr.length-1); } Public Static Boolean childrecursion(int[] arr,intLeftintright) {//Reach leaf node if(left = right) {return true; }intMID =0;BooleanLeftchildend =false; for(inti = left; I<right; i++) {//left to right, encountered the first node larger than the root node value, that is, to reach the left subtree boundary (right subtree start) if(!leftchildend && arr[i] > Arr[right]) {mid = i; Leftchildend =true; }//If there is a node in the right subtree that has a lower value than the root node, it is directly determined to be false Else if(Leftchildend && Arr[i] < Arr[right]) {return false; } }//Left dial hand tree is empty if(Mid = = left) {returnChildrecursion (arr,mid,right-1); }//Right Sub-tree is empty Else if(Mid = right) {returnChildrecursion (arr,left,mid-1); }Else returnChildrecursion (arr,left,mid-1) && Childrecursion (arr,mid,right-1); } Public Static void PrintArray(int[] arr) { for(intI:arr) {System.out.print (i+" "); } } Public Static void Main(string[] args) {int[] arr = {5,7,6,9, One,Ten}; Test009.printarray (arr); System.out.println ("is a sequential sequence of a binary lookup tree:"+test009.existcorrespondbstree (arr) +"\ n");int[] arr2 = {7,4,6,5}; Test009.printarray (ARR2); System.out.println ("is a sequential sequence of a binary lookup tree:"+test009.existcorrespondbstree (ARR2) +"\ n");int[] Arr3 = {7, One, -, -, the,Wuyi, -, -}; Test009.printarray (ARR3); System.out.println ("is a sequential sequence of a binary lookup tree:"+test009.existcorrespondbstree (ARR3)); }}
Execution results
Connected to theTarget VM, Address:' 127.0.0.1:2715 ', Transport:' socket 'Disconnected from theTarget VM, Address:' 127.0.0.1:2715 ', Transport:' socket '5 7 6 9 One Tenis a sequential sequence of a binary lookup tree:true7 4 6 5is a sequential sequence of a binary lookup tree:false7 One - - the Wuyi - -is a sequential sequence of a binary lookup tree:trueProcess finished withExit code0
Question 9th: Judging the integer sequence is not a two-dollar lookup tree post-order traversal results