forked from xurui1995/Sword-pointing-to-offer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
No24.java
53 lines (40 loc) · 934 Bytes
/
No24.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class No24 {
/**
* 输入一个整数数组,
* 判断该数组是不是某二叉搜索树的后序遍历的结果。
* 如果是则返回true,否则返回false。
* 假设输入的数组的任意两个数字都互不相同
*/
public static void main(String[] args) {
int[] array={5,7,6,9,11,10,8};
// int[] array={7,4,6,5};
boolean b=verfiySequenceOfBST(array,0,6);
System.out.println(b);
}
private static boolean verfiySequenceOfBST(int[] array,int start ,int end) {
if(array==null||start>end||start<0||end<0)
return false;
if(start==end)
return true;
int root=array[end];
int i=start;
for(;i<=end;i++){
if(array[i]>root)
break;
}
int j=i;
for(;j<=end;j++){
if(array[j]<root)
return false;
}
boolean left=true;
if(i>start){
left=verfiySequenceOfBST(array,start,i-1);
}
boolean right=true;
if(i<end){
right=verfiySequenceOfBST(array,i,end-1);
}
return (left&&right);
}
}