leetcode “297. serialize and deserialize binary tree”

LeetCode link

first thought

  • Consider it as a complete binary tree.
  • Using the relation between level and index in the CBT.

solution

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
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
if (root == null) {
return "";
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int level = 1; // from 1
Map<TreeNode, Integer> indexMap = new HashMap<>();
Map<Integer, String> stringMap = new HashMap<>();
indexMap.put(root, 1); // index from 1
while (!queue.isEmpty()) {
Queue<TreeNode> queue2 = new LinkedList<>();
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
int index = indexMap.get(node);
stringMap.put(index, String.valueOf(node.val));
if (node.left != null) {
queue2.add(node.left);
indexMap.put(node.left, index * 2);
}
if (node.right != null) {
queue2.add(node.right);
indexMap.put(node.right, index * 2 + 1);
}
}
if (!queue2.isEmpty()) {
queue = queue2;
level++;
}
}
StringBuilder builder = new StringBuilder();
builder.append(stringMap.get(1));
for (int i = 2; i < Math.pow(2, level); i++) {
builder.append(" ");
if (stringMap.containsKey(i)) {
builder.append(stringMap.get(i));
} else {
builder.append("null");
}
}
// System.out.println(builder.toString());
return builder.toString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.length() < 1 || data.equals("null")) {
return null;
}
String[] s = data.split(" ");
TreeNode root = new TreeNode(Integer.parseInt(s[0]));
this.dfs(s, root, 1);
return root;
}
private void dfs(String[] s, TreeNode root, int index) {
if (index * 2 + 1 > s.length || root == null) {
return;
}
String left = s[index * 2 - 1];
if (!left.equals("null")) {
TreeNode leftNode = new TreeNode(Integer.parseInt(left));
this.dfs(s, leftNode, index * 2);
root.left = leftNode;
}
String right = s[index * 2];
if (!right.equals("null")) {
TreeNode rightNode = new TreeNode(Integer.parseInt(right));
this.dfs(s, rightNode, index * 2 + 1);
root.right = rightNode;
}
}
}

problem

MLE. 46/47

reason

There’s too many objects to cache the states of this tree.

You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
I’m narrow myself in the LeetCode format.


try

  • And then I consider it as build a tree using preorder and inorder, but after a while I found out that the nodes may be duplicated, so the order can not tell the structure of the tree(think a tree which all values are the same)

modification

  • Have seen the answer, this problem is just a simple preorder problem.
  • Create the string in preorder.
  • The construction of the node is also a preorder-like DFS.
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
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder builder = new StringBuilder();
this.preorder(root, builder);
return builder.toString();
}
private void preorder(TreeNode root, StringBuilder builder) {
if (root == null) {
builder.append("n").append(" ");
return;
}
builder.append(String.valueOf(root.val)).append(" ");
this.preorder(root.left, builder);
this.preorder(root.right, builder);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.length() < 1) {
return null;
}
Queue<String> queue = new LinkedList<>();
queue.addAll(Arrays.asList(data.split(" ")));
return this.dfs(queue);
}
private TreeNode dfs(Queue<String> queue) {
if (queue.isEmpty()) {
return null;
}
String str = queue.poll();
if (str.equals("n") || str.length() < 1) {
return null;
}
TreeNode node = new TreeNode(Integer.parseInt(str));
node.left = this.dfs(queue);
node.right = this.dfs(queue);
return node;
}
}