Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored or transmitted. 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.
Example 1
Input: root = [1,null,3,2,4,null,5,6]
Output: [1,null,3,2,4,null,5,6]
Explanation: The root 1 has children 3, 2 and 4, and node 3 has children 5 and 6. Each null marks the end of a child list. Encoding and decoding gives back the same tree.
Example 2
Input: root = [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]
Output: [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]
Explanation: This deeper tree has nodes with different numbers of children, and some have none. The round trip has to keep every child list and every null separator in place, so the output matches the input exactly.
Example 3
Input: root = []
Output: []
Explanation: An empty tree encodes and decodes to an empty tree.
Serialize using preorder DFS, recording each node's value followed by its child count. This uniquely defines the tree structure.
Run checks all cases above. Submit evaluates all test cases.