431. Encode N-ary Tree to Binary Tree

Problem

Encode an ordered N-ary tree into a binary tree and decode it back without losing structure.

The platform adapts the classic N-ary to binary encoding design task into a stateless pure function. It supplies data, the preorder serialization of an ordered N-ary tree as a flat integer array, and expects the level-order serialization of the decoded tree.

The flat serialization uses two consecutive integers per node: its value, then its number of children, in preorder. The output uses the same value and child-count pairs, but in level order.

The callable must actually perform the reversible left-child right-sibling mapping. The first N-ary child of a node becomes that node's left child, and every later child becomes the right child of the previous child. Decoding walks the left child and then follows the right-sibling chain to recover the complete ordered child list. Because the mapping is lossless, the decoded tree has the same values and child order as the input.

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

431. Encode N-ary Tree to Binary Tree

Encode an ordered N-ary tree into a binary tree and decode it back without losing structure.