-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSerializeAndDeserializeABinaryTree.java
69 lines (58 loc) · 1.7 KB
/
SerializeAndDeserializeABinaryTree.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
61
62
63
64
65
66
67
68
69
/*Complete the given function
Node is as follows:
class Tree{
int data;
Tree left,right;
Tree(int d){
data=d;
left=right=null;
}
}*/
class Tree {
// Function to serialize a tree and return a list containing nodes of the tree.
public ArrayList<Integer> serialize(Node root) {
ArrayList<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
Node cur = queue.poll();
if (cur != null) {
result.add(cur.data);
queue.offer(cur.left);
queue.offer(cur.right);
} else {
result.add(-1); // Placeholder for null nodes
}
}
return result;
}
// Function to deserialize a list and construct the tree.
public Node deSerialize(ArrayList<Integer> A) {
if (A == null || A.isEmpty()) {
return null;
}
int i = 0;
Node root = new Node(A.get(i++));
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
Node cur = queue.poll();
// left tree construct
int leftValue = A.get(i++);
if (leftValue != -1) {
cur.left = new Node(leftValue);
queue.offer(cur.left);
}
// right tree construct
int rightValue = A.get(i++);
if (rightValue != -1) {
cur.right = new Node(rightValue);
queue.offer(cur.right);
}
}
return root;
}
}