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 following 3-ary
tree
as [1 [3[5 6] 2 4]]
. Note that this is just an example, you do not necessarily need to follow this format.
Or you can follow LeetCode's level order traversal serialization format, where each group of children is separated by the null value.
For example, the above tree may be serialized as [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]
.
You do not necessarily need to follow the above suggested formats, there are many more different formats that work so please be creative and come up with different approaches yourself.
Constraints:
- The height of the n-ary tree is less than or equal to
1000
- The total number of nodes is between
[0, 10^4]
- Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
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 | class Codec { void sh(Node* node, string& s){ if(node){ s += to_string(node->val) + " " + to_string(node->children.size()) + " "; for(auto c:node->children){ sh(c, s); } } } Node* dh(stringstream& ss){ ss.peek(); if(ss.eof()) return NULL; int sz; Node* node = new Node(); ss>>node->val>>sz; for(int i=0; i<sz; ++i){ node->children.push_back(dh(ss)); } return node; } public: // Encodes a tree to a single string. string serialize(Node* root) { string s; sh(root, s); return s; } // Decodes your encoded data to tree. Node* deserialize(string data) { stringstream ss(data); return dh(ss); } }; |
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 | /* // Definition for a Node. class Node { public int val; public List<Node> children; public Node() {} public Node(int _val) { val = _val; } public Node(int _val, List<Node> _children) { val = _val; children = _children; } }; */ class Codec { void sh(Node root, StringBuilder sb){ if(root!=null){ sb.append(root.val); sb.append(" "); sb.append(root.children.size()); sb.append(" "); for(Node child : root.children){ sh(child, sb); } } } // Encodes a tree to a single string. public String serialize(Node root) { StringBuilder sb = new StringBuilder(); sh(root, sb); return sb.toString(); } // Decodes your encoded data to tree. Node dh(StringBuilder sb){ if(sb.length()==0) return null; int indexOfFirstSpace = sb.indexOf(" ", 0); int number = Integer.parseInt(sb.substring(0, indexOfFirstSpace)); Node node = new Node(number, new ArrayList<>()); int indexOfSecondSpace = sb.indexOf(" ", indexOfFirstSpace+1); if(indexOfSecondSpace==-1) indexOfSecondSpace = sb.length(); int sz = Integer.parseInt(sb.substring(indexOfFirstSpace+1, indexOfSecondSpace)); int newStart = indexOfSecondSpace!=sb.length() ? indexOfSecondSpace+1 : indexOfSecondSpace; sb.delete(0, newStart); for(int i=0; i<sz; ++i){ node.children.add(dh(sb)); } return node; } public Node deserialize(String data) { StringBuilder sb = new StringBuilder(data); return dh(sb); } } // Your Codec object will be instantiated and called as such: // Codec codec = new Codec(); // codec.deserialize(codec.serialize(root)); |