-
Notifications
You must be signed in to change notification settings - Fork 22
/
SerializeAndDeserializeTree.java
48 lines (42 loc) · 1.55 KB
/
SerializeAndDeserializeTree.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
package com.company.amazon;
import com.company.amazon.BinaryTree.Node;
import java.util.ArrayList;
import java.util.List;
/**
* Problem : 179 Amazon Interview Questions
*/
public class SerializeAndDeserializeTree {
static int index = 0;
public static void main(String[] args) {
Node tree = ConstructBSTFromPreOrder.constructBST(new int[]{10, 5, 1, 7, 40, 50});
System.out.println("Before Serializing.............");
BinaryTree.inorder(tree, true);
List<String> serializedData = new ArrayList<>();
System.out.println("Doing Serializing.............");
serialize(serializedData, tree);
System.out.println(serializedData);
System.out.println("While De-Serializing.............");
tree = deserialize(serializedData);
BinaryTree.inorder(tree, true);
}
private static Node deserialize(List<String> serializedData) {
if (index >= serializedData.size() || serializedData.get(index).equalsIgnoreCase("#")) {
index++; // Skip if Hash Encountered
return null;
}
Node root = new Node(Integer.parseInt(serializedData.get(index)));
index++;
root.left = deserialize(serializedData);
root.right = deserialize(serializedData);
return root;
}
public static void serialize(List<String> list, Node root) {
if (root == null) {
list.add("#");
return;
}
list.add(String.valueOf(root.data));
serialize(list, root.left);
serialize(list, root.right);
}
}