-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodec.java
More file actions
39 lines (34 loc) · 1.22 KB
/
Copy pathCodec.java
File metadata and controls
39 lines (34 loc) · 1.22 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
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if (root == null) return "";
StringBuilder sb = new StringBuilder();
serialize(root, sb);
return sb.toString();
}
private void serialize(TreeNode root, StringBuilder sb){
if (root == null) return;
sb.append(root.val).append(",");
serialize(root.left, sb);
serialize(root.right, sb);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.isEmpty()) return null;
Queue<String> queue = new LinkedList<>(Arrays.asList(data.split(",")));
return deserialize(queue, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private TreeNode deserialize(Queue<String> queue, int min, int max){
if (queue.isEmpty()) return null;
int val = Integer.parseInt(queue.peek());
if (val < min || val > max) return null;
queue.poll();
TreeNode root = new TreeNode(val);
root.left = deserialize(queue, min, val);
root.right = deserialize(queue, val, max);
return root;
}
}