forked from shijiebei2009/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerifySequenceOfBST.java
60 lines (57 loc) · 1.92 KB
/
VerifySequenceOfBST.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
54
55
56
57
58
59
60
package cn.codepub.algorithms.trees;
import java.util.Arrays;
/**
* <p>
* Created with IntelliJ IDEA. 2015/10/30 14:56
* </p>
* <p>
* ClassName:VerifySequenceOfBST
* </p>
* <p>
* Description:
* 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果,如果是则返回true,否则返回false
* 假设输入的数组的任意两个数字都互不相同
* </P>
*
* @author Wang Xu
* @version V1.0.0
* @since V1.0.0
*/
public class VerifySequenceOfBST {
public static void main(String[] args) {
int[] nums = new int[]{5, 7, 6, 9, 11, 10, 8};
boolean verify = verify(nums);
System.out.println(verify);
nums = new int[]{7, 4, 6, 5};
verify = verify(nums);
System.out.println(verify);
}
public static boolean verify(int[] sequences) {
if (null == sequences || sequences.length <= 1) {
return true;
}
int root = sequences[sequences.length - 1];
int index = 0;
//寻找左右子树的分割点
while (index < sequences.length && sequences[index] < root) {
index++;
}
//校验右子树应该是全大于root值
for (int i = index; i < sequences.length; i++) {
if (sequences[i] < root) {
return false;
}
}
boolean left = true;
//如果index==0,说明根节点之前所有元素都属于右子树,无需判断左子树
if (index > 0) {
left = verify(Arrays.copyOf(sequences, index));
}
boolean right = true;
//如果index>=length-1,根节点之前所有元素都属于左子树,无需判断右子树
if (index < sequences.length - 1) {
right = verify(Arrays.copyOfRange(sequences, index, sequences.length - 1));
}
return left && right;
}
}