forked from xiaoningning/java-algorithm-2010
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareArrayBST.java
More file actions
79 lines (65 loc) · 2.17 KB
/
CompareArrayBST.java
File metadata and controls
79 lines (65 loc) · 2.17 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.ArrayList;
/**
* Given two integer unsorted arrays, your task is
* to compare the BST formed by both the arrays.
* <p/>
* e.g [3 2 1 0 5 4 6] & [3 5 2 6 4 1 0]
* when the BST is formed by taking the elements from the left,
* both BST turn out to be same. (in-order)
* <p/>
* 3
* / \
* 2 5
* / \ / \
* 1 0 4 6
* <p/>
* Expected time complexity O(n).
*/
public class CompareArrayBST {
public static void main(String[] args) {
int[] a1 = new int[]{3, 2, 1, 0, 5, 4, 6};
int[] a2 = new int[]{3, 5, 2, 6, 4, 1, 0};
boolean same = identicalBST(a1, a2);
System.out.println(same);
int[] b1 = new int[]{3, 2, 1, 0, 5, 4, 6};
int[] b2 = new int[]{3, 6, 2, 5, 4, 1, 0};
boolean same1 = identicalBST(b1, b2);
System.out.println(same1);
}
public static boolean identicalBST(int[] a, int[] b) {
if (a.length != b.length)
return false;
if (a.length == 0)
return true;
if (a.length == 1 && a[0] == b[0])
return true;
if (a[0] != b[0])
return false;
ArrayList<Integer> aSmaller = new ArrayList<Integer>();
ArrayList<Integer> aLarger = new ArrayList<Integer>();
ArrayList<Integer> bSmaller = new ArrayList<Integer>();
ArrayList<Integer> bLarger = new ArrayList<Integer>();
//a[0] and b[0] are the roots of trees.
for (int i = 1; i < a.length; i++) {
if (a[i] < a[0])
aSmaller.add(a[i]);
else
aLarger.add(a[i]);
}
for (int i = 1; i < b.length; i++) {
if (b[i] < b[0])
bSmaller.add(b[i]);
else
bLarger.add(b[i]);
}
return (identicalBST(convertIntArray(aSmaller), convertIntArray(bSmaller))
&& identicalBST(convertIntArray(aLarger), convertIntArray(bLarger)));
}
public static int[] convertIntArray(ArrayList<Integer> integers) {
int[] ret = new int[integers.size()];
for (int i = 0; i < ret.length; i++) {
ret[i] = integers.get(i).intValue();
}
return ret;
}
}