标签:
1 public class Solution { 2 public boolean VerifySquenceOfBST(int [] sequence) { 3 boolean res = false ; 4 if(sequence.length==0||sequence==null){ 5 return res ; 6 } 7 else if(sequence.length==1){ 8 res = true ; 9 return res ; 10 } 11 else res = verifyQquenceOfBST(sequence ,0,sequence.length-1) ; 12 return res ; 13 } 14 public boolean verifyQquenceOfBST (int[] array ,int begin,int end ){ 15 boolean res = false ; 16 if(begin==end){ 17 return true ; 18 } 19 int mid = begin ; 20 while(array[mid]<array[end]&&mid<end){ 21 mid++ ; 22 } 23 if(mid==end){ 24 res = verifyQquenceOfBST ( array ,begin,end-1 ) ; 25 } 26 else if(mid==begin){ 27 for(int i = begin ; i<end ;i++){ 28 if(array[i]<array[end]){ 29 return false ; 30 } 31 32 } 33 res = verifyQquenceOfBST ( array ,begin,end-1 ) ; 34 } 35 else{ 36 for(int i = mid ; i<end ;i++){ 37 if(array[i]<array[end]){ 38 return false ; 39 } 40 } 41 res = verifyQquenceOfBST ( array ,begin,mid-1 ) &&verifyQquenceOfBST ( array ,mid,end-1 ) ; 42 } 43 return res ; 44 } 45 }
标签:
原文地址:http://www.cnblogs.com/huntertoung/p/4800968.html