-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGT_construct_tree_from_array_2.java
60 lines (49 loc) · 1.49 KB
/
GT_construct_tree_from_array_2.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
import java.io.*;
import java.util.*;
public class GT_construct_tree_from_array_2 {
private static class Node {
int data;
ArrayList<Node> children = new ArrayList<>();
}
public static void display(Node node) {
System.out.println(node.data);
}
public static Node construct(int[] arr) {
//psuedocode
//take a stack
//if stack empty, the node is root
//
//if(arr[i]==-1) pop and add tos.children.add(pop)
//else create a node, push it in stack
Stack<Node> stack = new Stack<>();
Node root = null;
Node node = new Node();
for(int i:arr){
if(i!=-1){
node.data = i;
stack.push(node);
}
else{
Node pop = stack.pop();
if(stack.size()>0){
stack.peek().children.add(pop);
}
else{
root = node;
}
}
}
return root;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
String[] values = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(values[i]);
}
Node root = construct(arr);
display(root);
}
}