Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize an N-ary tree. An N-ary tree is a rooted tree in which each node has no more than N children. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that an N-ary tree can be serialized to a string and this string can be deserialized to the original tree structure.
For example, you may serialize the following3-arytree
This is a template that can be applied to serialize and deserialize both binary and n-ary trees.
The only difference is that to serialize n-ary tree, we need to append the number of children of a node.
Serialize output with example as input:
1,3,3,2,5,0,6,0,2,0,4,0,
--
/*// Definition for a Node.class Node { public int val; public List<Node> children; public Node() {} public Node(int _val,List<Node> _children) { val = _val; children = _children; }};*/classCodec {// Encodes a tree to a single string.publicStringserialize(Node root) {StringBuilder sb =newStringBuilder();serial(sb, root);// System.out.println(sb.toString());returnsb.toString(); }privatevoidserial(StringBuilder sb,Node root) {if (root ==null) {sb.append("#");sb.append(","); } else {sb.append(root.val);sb.append(",");if (root.children!=null) {sb.append(root.children.size());sb.append(",");for (Node child:root.children) {serial(sb, child); } } } }// Decodes your encoded data to tree.publicNodedeserialize(String data) {Queue<String> queue =newLinkedList<String>(Arrays.asList(data.split(",")));returnbuildTree(queue); }privateNodebuildTree(Queue<String> queue) {String val =queue.poll();if (val.equals("#")) {returnnull; }Node root =newNode(Integer.parseInt(val));int childrenCount =Integer.parseInt(queue.poll());root.children=newArrayList<Node>();for (int i =0; i < childrenCount; i++) {root.children.add(buildTree(queue)); }return root; }}// Your Codec object will be instantiated and called as such:// Codec codec = new Codec();// codec.deserialize(codec.serialize(root));
classCodec {// Encodes a tree to a single string.publicStringserialize(Node root) {if (root ==null) {return""; }StringBuilder sb =newStringBuilder();sb.append(root.val);sb.append("[");for (Node child :root.children) {sb.append(serialize(child)); }sb.append("]");returnsb.toString(); }// Decodes your encoded data to tree.publicNodedeserialize(String data) {Node root =null;Stack<Node> stack =newStack<>();int i =0;while (i <data.length()) {int start = i;// Move pointer forward until we don't find a digit...while (i <data.length() &&Character.isDigit(data.charAt(i))) { i++; }// If we haven't found a digit then we must have found the end of a child list...if (start == i) {Node child =stack.pop();if (stack.isEmpty()) { root = child;break; } else {// Remove the child from the stack and assign it to the previous node on the stackstack.peek().children.add(child); } } else {Node n =newNode();n.val=Integer.parseInt(data.substring(start, i));n.children=newArrayList<>();stack.push(n); } i++; }return root; }}