-
Notifications
You must be signed in to change notification settings - Fork 1
/
Offer33.java
32 lines (30 loc) · 1.08 KB
/
Offer33.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
package lcof;
/**
* Offer33. 二叉搜索树的后序遍历序列
* 对于 postorder,判断整棵树是不是平衡二叉树,首先明白 最后一个数即为根节点。
* * 根据根节点的值,找出左子树(左子树所有的值都比根节点小)
* * 然后判断右子树是不是都比根节点大(否则可以直接返回false)
* * 然后递归判断左右子树是否满足即可
* @author LBW
*/
public class Offer33 {
public boolean verifyPostorder(int[] postorder) {
int n = postorder.length;
return verifyPostorder(postorder, 0, n - 1);
}
private boolean verifyPostorder(int[] postorder, int start, int end) {
if (start >= end) {
return true;
}
int mid = start;
while (postorder[mid] < postorder[end]) {
mid += 1;
}
for (int i = mid; i < end; i++) {
if (postorder[i] < postorder[end]) {
return false;
}
}
return verifyPostorder(postorder, start, mid - 1) && verifyPostorder(postorder, mid, end - 1);
}
}