-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution33.java
More file actions
27 lines (25 loc) · 841 Bytes
/
Solution33.java
File metadata and controls
27 lines (25 loc) · 841 Bytes
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
class Solution {
public boolean verifyPostorder(int[] data) {
// 空树
if (data == null || data.length == 0)
return true;
return verifySquenceOfBST(data, 0, data.length - 1);
}
public boolean verifySquenceOfBST(int[] data, int start, int end) {
// 数组长度为2,一定能够组成BST
if (end - start <= 1)
return true;
int root = data[end];
int rightStart = 0;
for (; rightStart < end; rightStart++) {
if (data[rightStart] > root) {
break;
}
}
for (int i = rightStart; i < end; i++) {
if (data[i] < root)
return false;
}
return verifySquenceOfBST(data, start, rightStart - 1) && verifySquenceOfBST(data, rightStart, end - 1);
}
}