Encode N-ary Tree to Binary Tree
Last updated
Last updated
Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the original N-ary tree. An N-ary tree is a rooted tree in which each node has no more than N children. Similarly, a binary tree is a rooted tree in which each node has no more than 2 children. There is no restriction on how your encode/decode algorithm should work. You just need to ensure that an N-ary tree can be encoded to a binary tree and this binary tree can be decoded to the original N-nary tree structure.
For example, you may encode the following3-ary
tree to a binary tree in this way:
Note that the above is just an example which_might or might not_work. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note:
N
is in the range of [1, 1000]
Do not use class member/global/static variables to store states. Your encode and decode algorithms should be stateless.
There are a lot of solutions to convert a N-ary tree to a binary tree. We only introduce one classical solution.
The strategy follows two rules:
Theleft child
of each node in the binary tree isthe next sibling
of the node in the N-ary tree.
Theright child
of each node in the binary tree isthe first child
of the node in the N-ary tree.
Using this strategy, you can convert a N-ary tree to a binary tree recursively. Also, you can recover the N-ary tree from the binary tree you converted.
The recursion recovery strategy for each node is:
Deal with its children recursively.
Add itsleft child
asthe next child of its parent
if it has a left child.
Add itsright child
asthe first child of the node itself
if it has a right child.