LeetCode 428 Serialize and Deserialize N-ary Tree
serialize is from a Node root to String
and deserialize is from String to Node root.
looks like this problem is similar as S and D binary tree, but there are not the same.
because we don’t know how many branches in each node so we can’t simple add null.
however, the problem doesn’t ask for we have to fill all the nulls like we used to. so we can either:

[1 [3[5 6] 2 4]] //nested

[1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14] //add null between each child group
so at first, i was thinking write some codes like D and S of binary tree.
but when I deserialize things are not right.
class Codec {
// Encodes a tree to a single string.
public String serialize(Node root) {
if (root == null) return "";
StringBuilder res = new StringBuilder();
Queue<TreeNode> queue = new LinkedList;
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
res.append(Integer.toString(cur.val) + " ");
int size = cur.children.size();
for (Node child: cur.children) {
if (child != null) queue.offer(child);
res.append(Integer.toString(child.val) + " ");
}
res.append("null ");
}
return res.toString().trim();
}
// Decodes your encoded data to tree.
public Node deserialize(String data) {
if (data == null || data.length() == 0) return null;
String[] values = data.split(" ");
Queue<TreeNode> queue = new LinkedList<>();
TreeNode root = new TreeNode(Integer.parseInt(values[0]));
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
if ...// i found problem here, because I only use null to divide different child group but we don't know which belongs to which we deserialized
}
}
}
but actually, the idea off this problem is relatively easy.
but how do we design our ser and deser to make it can be reverseable.
class Codec {
// Encodes a tree to a single string.
public String serialize(Node root) {
if (root == null) return "";
StringBuilder sb = new StringBuilder();
Queue<Node> queue = new LinkedList<>();
sb.append(Integer.toString(root.val));
sb.append(" ");
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Node curr = queue.poll();
if (curr != null) {
List<Node> children = curr.children;
if (children == null || children.size() == 0) {
sb.append("null");
} else {
for (Node child: children) {
sb.append(Integer.toString(child.val));
sb.append(",");
queue.offer(child);
}
}
sb.append(" ");
}
}
}
return sb.toString().trim();
}
// Decodes your encoded data to tree.
public Node deserialize(String data) {
if (data == null || data.length() == 0) return null;
Queue<Node> parents = new LinkedList<>();
String[] elements = data.split(" ");
Node root = new Node(Integer.valueOf(elements[0]), null);
parents.offer(root);
for (int i = 1; i < elements.length; i++) {
Node parent = parents.poll();
String[] kids = elements[i].split(",");
List<Node> c= new ArrayList<>();
for (String kid: kids) {
if (kid.length() == 0) continue;
if (kid.equals("null")) continue;
Node k = new Node(Integer.valueOf(kid), null);
c.add(k);
parents.offer(k);
}
parent.children = c;
}
return root;
}
}

浙公网安备 33010602011771号