Skip to content

Commit 94b882d

Browse files
committed
array is bst post order
1 parent 44a5a2c commit 94b882d

2 files changed

Lines changed: 64 additions & 35 deletions

File tree

PuzzleCoding/src/CompareArrayBST.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,76 @@
11
import java.util.ArrayList;
22

33
/**
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).
1718
*/
19+
1820
public class CompareArrayBST {
19-
public static void main(String[] args){
21+
public static void main(String[] args) {
2022

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};
2325

2426
boolean same = identicalBST(a1, a2);
2527

2628
System.out.println(same);
2729

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};
3032

3133
boolean same1 = identicalBST(b1, b2);
3234

3335
System.out.println(same1);
3436
}
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)
3740
return false;
38-
if(a.length == 0)
41+
if (a.length == 0)
3942
return true;
40-
if(a.length == 1 && a[0]==b[0])
43+
if (a.length == 1 && a[0] == b[0])
4144
return true;
42-
if(a[0] != b[0])
45+
if (a[0] != b[0])
4346
return false;
4447

4548
ArrayList<Integer> aSmaller = new ArrayList<Integer>();
4649
ArrayList<Integer> aLarger = new ArrayList<Integer>();
4750
ArrayList<Integer> bSmaller = new ArrayList<Integer>();
4851
ArrayList<Integer> bLarger = new ArrayList<Integer>();
4952

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]);
5356
else
54-
aLarger.add(a[i]);
57+
aLarger.add(a[i]);
5558
}
5659

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])
5962
bSmaller.add(b[i]);
6063
else
6164
bLarger.add(b[i]);
6265
}
6366

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)));
6669
}
6770

68-
public static int[] convertIntArray(ArrayList<Integer> integers)
69-
{
71+
public static int[] convertIntArray(ArrayList<Integer> integers) {
7072
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++) {
7374
ret[i] = integers.get(i).intValue();
7475
}
7576
return ret;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Is array post order of BST?
3+
* post order: left, right, root
4+
*/
5+
public class IsBSTPostOrder {
6+
public static void main(String[] args){
7+
int[] a = new int[]{0,2,1,4,5,3};
8+
boolean postOrder = isBstPostOrder(a, 0, a.length-1);
9+
System.out.println(postOrder);
10+
}
11+
public static boolean isBstPostOrder(int[] a, int left, int right){
12+
if(left > right)
13+
return false;
14+
if(right - left ==1 || right ==left)
15+
return true;
16+
int i = left;
17+
while(a[i] < a[right])
18+
i++;
19+
20+
for(int j = i; j <= right; j++){
21+
if(a[j]<a[right])
22+
return false;
23+
}
24+
25+
return isBstPostOrder(a, left, i-1) && isBstPostOrder(a, i, right-1);
26+
27+
}
28+
}

0 commit comments

Comments
 (0)