-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
68 lines (59 loc) · 1.71 KB
/
BST.java
File metadata and controls
68 lines (59 loc) · 1.71 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
import java.util.*;
import java.lang.*;
import java.io.*;
class BST {
BST left;
BST right;
int data;
public BST() {
data = -1;
left = right = null;
}
public BST(int data) {
this.data = data;
left = right = null;
}
}
class GFG {
static BST root;
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
for (int t = 0; t < T; t++) {
int N = Integer.parseInt(br.readLine().trim());
String[] in = br.readLine().trim().split(" ");
int[] preorder = new int[in.length];
root = new BST(Integer.parseInt(in[0]));
for (int i = 0; i < in.length; i++) {
preorder[i] = Integer.parseInt(in[i]);
if (i != 0) {
BST node = new BST(preorder[i]);
insertNode(node);
}
}
printPostorder(root);
System.out.println();
}
}
private static void printPostorder(BST root) {
if (root == null) return;
printPostorder(root.left);
printPostorder(root.right);
System.out.print(root.data + " ");
}
private static void insertNode(BST node) {
BST r = root;
while (r != null) {
if (r.data < node.data) {
if (r.right == null) r.right = node;
else {
r = r.right;
continue;
}
} else {
if (r.left == null) r.left = node;
else r = r.left;
}
}
}
}