428. Serialize and Deserialize N-ary Tree

Problem

Serialize and deserialize an ordered N-ary tree without losing structure.

The platform adapts the classic serialize-and-deserialize design task into a stateless pure function. It supplies data, the preorder serialization of an ordered N-ary tree, and expects the level-order serialization of the same tree.

Both serializations use the same token grammar. Tokens are decimal integers separated by commas. Every node contributes exactly two consecutive tokens: its value, then its number of children. In the preorder input, a node is followed immediately by the tokens of its first child, then that child's subtree, and so on. In the level-order output, nodes are written breadth first, and a node's children are the next unassigned nodes in that same breadth-first order.

The empty tree is the empty string, and it maps to the empty string. Every other tree round trips losslessly: deserialize data into the ordered tree, then serialize that tree in level order.

Examples
Input: [[],0]
Output: [0,0,0,0,0]
Hints
Related Problems

428. Serialize and Deserialize N-ary Tree

Serialize and deserialize an ordered N-ary tree without losing structure.