-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLastOrderTree24.java
More file actions
56 lines (53 loc) · 1.83 KB
/
LastOrderTree24.java
File metadata and controls
56 lines (53 loc) · 1.83 KB
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
54
55
56
package offer;
import java.sql.Array;
import java.util.Arrays;
/*
* ��һ�����飬�ж��Dz��Ƕ����������ĺ����������
*/
public class LastOrderTree24 {
public boolean VerifySquenceOfBST(int [] sequence) {
if(sequence.length == 0){
return false;
}
//��ڵ�
int root = sequence[sequence.length - 1];
//�ҵ����ڸ�ڵ��λ��i
int i = 0;
for (i = 0; i < sequence.length - 1; i++) {
if(sequence[i] > root) {
break;
}
}
//����ҵ��˴��ڸ�ڵ��i������Ӧ [i---len-2] �� > root,
//���� ��Ȼ���Ǻ����������
for (int j = i; j < sequence.length; j++) {
if(sequence[j] < root){
return false;
}
}
//��ͬ���ж����������Dz��Ƿ���������
boolean left = true, right = true;
//��������
if(i > 0){
int [] leftarr = new int[i];
for (int j = 0; j < i; j++) {
leftarr[j] = sequence[j];
}
left = VerifySquenceOfBST(leftarr);
}
//��������
if(i < sequence.length - 1){
int [] rightarr = new int[sequence.length - 1 - i];
for (int j = 0; j < rightarr.length; j++) {
rightarr[j] = sequence[j];
}
right = VerifySquenceOfBST(rightarr);
}
return left&&right;
}
public static void main(String[] args) {
int [] a = {5, 7, 6, 9,11, 10 ,8};
boolean b = new LastOrderTree24().VerifySquenceOfBST(a);
System.out.println(b);
}
}