|
1 | 1 | import java.util.ArrayList; |
2 | 2 |
|
3 | 3 | /** |
4 | | - Given two integer unsorted arrays, your task is to compare the BST formed by both the arrays. |
5 | | -
|
6 | | - e.g [3 2 1 0 5 4 6] & [3 5 2 6 4 1 0] |
7 | | - when the BST is formed by taking the elements from the left, both BST turn out to be same. |
8 | | -
|
9 | | - 3 |
10 | | - / \ |
11 | | - 2 5 |
12 | | - / \ / \ |
13 | | - 1 0 4 6 |
14 | | -
|
15 | | - Expected time complexity O(n). |
16 | | -
|
| 4 | + * Given two integer unsorted arrays, your task is |
| 5 | + * to compare the BST formed by both the arrays. |
| 6 | + * <p/> |
| 7 | + * e.g [3 2 1 0 5 4 6] & [3 5 2 6 4 1 0] |
| 8 | + * when the BST is formed by taking the elements from the left, |
| 9 | + * both BST turn out to be same. |
| 10 | + * <p/> |
| 11 | + * 3 |
| 12 | + * / \ |
| 13 | + * 2 5 |
| 14 | + * / \ / \ |
| 15 | + * 1 0 4 6 |
| 16 | + * <p/> |
| 17 | + * Expected time complexity O(n). |
17 | 18 | */ |
| 19 | + |
18 | 20 | public class CompareArrayBST { |
19 | | - public static void main(String[] args){ |
| 21 | + public static void main(String[] args) { |
20 | 22 |
|
21 | | - int[] a1 = new int[]{3,2,1,0,5,4,6}; |
22 | | - int[] a2 = new int[]{3,5,2,6,4,1,0}; |
| 23 | + int[] a1 = new int[]{3, 2, 1, 0, 5, 4, 6}; |
| 24 | + int[] a2 = new int[]{3, 5, 2, 6, 4, 1, 0}; |
23 | 25 |
|
24 | 26 | boolean same = identicalBST(a1, a2); |
25 | 27 |
|
26 | 28 | System.out.println(same); |
27 | 29 |
|
28 | | - int[] b1 = new int[]{3,2,1,0,5,4,6}; |
29 | | - int[] b2 = new int[]{3,6,2,5,4,1,0}; |
| 30 | + int[] b1 = new int[]{3, 2, 1, 0, 5, 4, 6}; |
| 31 | + int[] b2 = new int[]{3, 6, 2, 5, 4, 1, 0}; |
30 | 32 |
|
31 | 33 | boolean same1 = identicalBST(b1, b2); |
32 | 34 |
|
33 | 35 | System.out.println(same1); |
34 | 36 | } |
35 | | - public static boolean identicalBST(int[] a, int[] b ){ |
36 | | - if(a.length != b.length) |
| 37 | + |
| 38 | + public static boolean identicalBST(int[] a, int[] b) { |
| 39 | + if (a.length != b.length) |
37 | 40 | return false; |
38 | | - if(a.length == 0) |
| 41 | + if (a.length == 0) |
39 | 42 | return true; |
40 | | - if(a.length == 1 && a[0]==b[0]) |
| 43 | + if (a.length == 1 && a[0] == b[0]) |
41 | 44 | return true; |
42 | | - if(a[0] != b[0]) |
| 45 | + if (a[0] != b[0]) |
43 | 46 | return false; |
44 | 47 |
|
45 | 48 | ArrayList<Integer> aSmaller = new ArrayList<Integer>(); |
46 | 49 | ArrayList<Integer> aLarger = new ArrayList<Integer>(); |
47 | 50 | ArrayList<Integer> bSmaller = new ArrayList<Integer>(); |
48 | 51 | ArrayList<Integer> bLarger = new ArrayList<Integer>(); |
49 | 52 |
|
50 | | - for(int i = 1; i < a.length;i++){ |
51 | | - if(a[i] < a[0]) |
52 | | - aSmaller.add(a[i]); |
| 53 | + for (int i = 1; i < a.length; i++) { |
| 54 | + if (a[i] < a[0]) |
| 55 | + aSmaller.add(a[i]); |
53 | 56 | else |
54 | | - aLarger.add(a[i]); |
| 57 | + aLarger.add(a[i]); |
55 | 58 | } |
56 | 59 |
|
57 | | - for(int i = 1; i < b.length;i++){ |
58 | | - if(b[i] < b[0]) |
| 60 | + for (int i = 1; i < b.length; i++) { |
| 61 | + if (b[i] < b[0]) |
59 | 62 | bSmaller.add(b[i]); |
60 | 63 | else |
61 | 64 | bLarger.add(b[i]); |
62 | 65 | } |
63 | 66 |
|
64 | | - return ( identicalBST(convertIntArray(aSmaller), convertIntArray(bSmaller)) |
65 | | - && identicalBST(convertIntArray(aLarger), convertIntArray(bLarger)) ) ; |
| 67 | + return (identicalBST(convertIntArray(aSmaller), convertIntArray(bSmaller)) |
| 68 | + && identicalBST(convertIntArray(aLarger), convertIntArray(bLarger))); |
66 | 69 | } |
67 | 70 |
|
68 | | - public static int[] convertIntArray(ArrayList<Integer> integers) |
69 | | - { |
| 71 | + public static int[] convertIntArray(ArrayList<Integer> integers) { |
70 | 72 | int[] ret = new int[integers.size()]; |
71 | | - for (int i=0; i < ret.length; i++) |
72 | | - { |
| 73 | + for (int i = 0; i < ret.length; i++) { |
73 | 74 | ret[i] = integers.get(i).intValue(); |
74 | 75 | } |
75 | 76 | return ret; |
|
0 commit comments