Serialize and Deserialize N-ary Tree - Problem

Serialization is the process of converting a complex data structure into a format that can be stored or transmitted and later reconstructed perfectly. Think of it like packing a complex 3D puzzle into a flat box - you need a systematic way to flatten it and rebuild it exactly as it was!

Your challenge is to design an algorithm to serialize (convert to string) and deserialize (reconstruct from string) an N-ary tree, where each node can have any number of children (not just 2 like binary trees).

Goal: Create two functions:

  • serialize(root) → converts tree to string
  • deserialize(data) → reconstructs tree from string

Example: A tree with root 1, children [3,2,4], and node 3 having children [5,6] could be serialized as "1[3[5,6],2,4]" or "1,null,3,2,4,5,6,null,null,null,null" - the format is entirely up to you!

Key requirement: deserialize(serialize(tree)) === tree

Input & Output

example_1.py — Basic N-ary Tree
$ Input: root = [1,null,3,2,4,5,6] Tree structure: 1 /|\ 3 2 4 /| 5 6
Output: serialize(root) = "1,3,3,2,5,0,6,0,2,0,4,0" deserialize("1,3,3,2,5,0,6,0,2,0,4,0") = original tree
💡 Note: The root node 1 has 3 children. Node 3 has 2 children (5,6). Nodes 2,4,5,6 are leaves with 0 children. The preorder traversal captures the structure perfectly.
example_2.py — Single Node Tree
$ Input: root = [1] Tree structure: 1
Output: serialize(root) = "1,0" deserialize("1,0") = Node(1) with empty children list
💡 Note: A single node tree serializes to just the value and children count (0). This is the simplest case - one node, no children.
example_3.py — Deep Linear Tree
$ Input: root represents: 1 | 2 | 3 | 4
Output: serialize(root) = "1,1,2,1,3,1,4,0" deserialize("1,1,2,1,3,1,4,0") = original linear tree
💡 Note: Each non-leaf node has exactly 1 child, creating a linear chain. The serialization shows: 1→1 child→2→1 child→3→1 child→4→0 children.

Constraints

  • The number of nodes in the tree is in the range [0, 104]
  • 0 ≤ Node.val ≤ 104
  • The height of the n-ary tree is less than or equal to 1000
  • No restriction on serialization format - be creative!
  • Must ensure deserialize(serialize(root)) == root

Visualization

Tap to expand
Serialize and Deserialize N-ary Tree INPUT: N-ary Tree 1 3 2 4 5 6 Input Array Format: [1,null,3,2,4,5,6] Each node has 0+ children Node 3 has children: 5, 6 Node 1 has children: 3, 2, 4 Preorder DFS traversal ALGORITHM STEPS 1 Serialize: DFS Preorder Visit node, store value + children count 2 Format: val,count Node 1 with 3 children becomes "1,3" 3 Deserialize: Parse Read value, then count Recursively build children 4 Reconstruct Tree Use count to know how many children to expect Serialization Order: 1,3 --> 3,2 --> 5,0 --> 6,0 --> 2,0 --> 4,0 Children count enables reconstruction Time: O(n) | Space: O(n) No null markers needed! FINAL RESULT Serialized String: "1,3,3,2,5,0,6,0,2,0,4,0" Deserialized Tree: 1 3 2 4 5 6 OK - Tree Reconstructed! Original tree == Deserialized tree Key Insight: By storing the children count with each node value, we eliminate the need for null markers or special delimiters. During deserialization, we know exactly how many children to recursively build for each node. This preorder DFS approach ensures O(n) time and space complexity for both serialize and deserialize operations. TutorialsPoint - Serialize and Deserialize N-ary Tree | Optimal Solution (Preorder DFS with Children Count)
Asked in
Google 35 Amazon 28 Facebook 22 Microsoft 18 Apple 12
89.3K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen