-
Notifications
You must be signed in to change notification settings - Fork 43
/
SerializeAndDeserializeBinaryTree.java
169 lines (138 loc) · 5.12 KB
/
SerializeAndDeserializeBinaryTree.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package LeetCodeJava.Tree;
// https://leetcode.com/problems/serialize-and-deserialize-binary-tree/description/?envType=list&envId=xoqag3yj
import LeetCodeJava.DataStructure.TreeNode;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
// Your Codec object will be instantiated and called as such:
// Codec ser = new Codec();
// Codec deser = new Codec();
// TreeNode ans = deser.deserialize(ser.serialize(root));
public class SerializeAndDeserializeBinaryTree {
// VO
// IDEA : DFS
public class Codec{
public String serialize(TreeNode root) {
/** NOTE !!!
*
* if root == null, return "#"
*/
if (root == null){
return "#";
}
/** NOTE !!! return result via pre-order, split with "," */
return root.val + "," + serialize(root.left) + "," + serialize(root.right);
}
public TreeNode deserialize(String data) {
/** NOTE !!!
*
* 1) init queue and append serialize output
* 2) even use queue, but helper func still using DFS
*/
Queue<String> queue = new LinkedList<>(Arrays.asList(data.split(",")));
return helper(queue);
}
private TreeNode helper(Queue<String> queue) {
// get val from queue first
String s = queue.poll();
if (s.equals("#")){
return null;
}
/** NOTE !!! init current node */
TreeNode root = new TreeNode(Integer.valueOf(s));
/** NOTE !!!
*
* since serialize is "pre-order",
* deserialize we use "pre-order" as well
* e.g. root -> left sub tree -> right sub tree
* -> so we get sub tree via below :
*
* root.left = helper(queue);
* root.right = helper(queue);
*
*/
root.left = helper(queue);
root.right = helper(queue);
/** NOTE !!! don't forget to return final deserialize result */
return root;
}
}
// V1
// IDEA : BFS + QUEUE OP
// https://leetcode.com/problems/serialize-and-deserialize-binary-tree/solutions/4705074/solution-using-level-order-traversal/?envType=list&envId=xoqag3yj
public class Codec_1 {
public String serialize(TreeNode root) {
if (root == null)
return "";
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
StringBuilder sb = new StringBuilder();
while (!q.isEmpty()) {
TreeNode node = q.poll();
if (node == null) {
sb.append("#").append(" ");
continue;
}
sb.append(node.val).append(" ");
q.add(node.left);
q.add(node.right);
}
return sb.toString().trim();
}
public TreeNode deserialize(String data) {
if (data.equals(""))
return null;
String[] nodes = data.split(" ");
TreeNode root = new TreeNode(Integer.parseInt(nodes[0]));
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int len = nodes.length;
for (int i = 1; i < len; i++) {
TreeNode parent = q.poll();
/** NOTE !!! if node[i] is NOT null ( nodes[i] != "#" ) */
if (! nodes[i].equals("#")) {
TreeNode leftNode = new TreeNode(Integer.parseInt(nodes[i]));
parent.left = leftNode;
q.add(leftNode);
}
/** NOTE !!! if node[++i] is NOT null ( node[++i] != "#" ) */
if (! nodes[++i].equals("#")) {
TreeNode rightNode = new TreeNode(Integer.parseInt(nodes[i]));
parent.right = rightNode;
q.add(rightNode);
}
}
return root;
}
}
// V1'
// IDEA : DFS
// https://leetcode.com/problems/serialize-and-deserialize-binary-tree/solutions/281714/clean-java-solution/?envType=list&envId=xoqag3yj
public class Codec_2 {
public String serialize(TreeNode root) {
if (root == null) return "#";
return root.val + "," + serialize(root.left) + "," + serialize(root.right);
}
public TreeNode deserialize(String data) {
Queue<String> queue = new LinkedList<>(Arrays.asList(data.split(",")));
return helper(queue);
}
private TreeNode helper(Queue<String> queue) {
String s = queue.poll();
if (s.equals("#")) return null;
TreeNode root = new TreeNode(Integer.valueOf(s));
root.left = helper(queue);
root.right = helper(queue);
return root;
}
}
}