Serialization and Deserialization of an N-ary Tree

Given an N-ary tree where each node has at most 'N' child nodes, the task is to serialize the tree into a sequence of bits and then deserialize it back to reconstruct the original tree.

Serialization involves converting a data structure or object into a format that can be stored or transmitted, while deserialization involves reconstructing the data structure from this format.

Input:

The first line of input contains an integer 'T', denoting the number of test cases. 
Each test case consists of elements representing the N-ary tree in a level-order format.
'-1' indicates end of children for a parent node.
The input ends when all nodes at the last level are -1.

Output:

For each test case, output the structure of the deserialized tree after serialization. Each test case result should be on a new line.

Example:

Consider an N-ary tree that is given in level order as follows:

Input: 1 -1 2 3 4 -1 5 6 -1 -1 -1 -1 -1

The deserialized output of this tree should maintain the original tree structure.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^4

Time limit: 1 second.

Note:
The format provided allows for clarity on the structure of the input tree, though is to be provided in a single, space-separated sequence for processing.
Be the first one to answer
Add answer anonymously...
Goldman Sachs Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter