Dataford
Interview Guides
Upgrade
All questions/Coding/Serialize and Deserialize Binary Tree

Serialize and Deserialize Binary Tree

Hard
Coding
Asked at 4 companies4TreesRecursionQueue
Also asked at
MetaMicrosoftAllscriptsC

Problem

Problem

In a Meta-style systems setting such as caching tree-structured ranking features for Facebook Feed, you may need to convert a binary tree into a compact string and later rebuild the exact same tree. Implement both serialization and deserialization.

Given the root of a binary tree, write two functions:

  1. serialize(root) that converts the tree into a string.
  2. deserialize(data) that reconstructs the original binary tree from that string.

Your encoding must preserve both node values and structure, including missing children.

Formal Specification

  • Input to serialize: root, the root node of a binary tree or null
  • Output from serialize: a string representation of the tree
  • Input to deserialize: data, a string produced by serialize
  • Output from deserialize: the reconstructed root node or null

You may choose any deterministic format, but deserialize(serialize(root)) must produce a tree identical to the original.

Examples

Example 1

  • Input: root = [1,2,3,null,null,4,5]
  • Output: "1,2,#,#,3,4,#,#,5,#,#"
  • Explanation: Preorder traversal with # markers preserves missing children.

Example 2

  • Input: root = []
  • Output: "#"
  • Explanation: An empty tree is represented by a single null marker.

Constraints

  • Number of nodes is in the range [0, 10^4]
  • -1000 <= Node.val <= 1000
  • The tree may be unbalanced
  • The output format must be lossless and deterministic

Examples

Example 1
Inputroot = [1,2,3,null,null,4,5]Output"1,2,#,#,3,4,#,#,5,#,#"WhyPreorder records each node and each missing child, so the exact original structure can be reconstructed.
Example 2
Inputroot = []Output"#"WhyAn empty tree has no nodes, so a single null marker is sufficient.
Example 3
Inputroot = [7,-3,null,null,9]Output"7,-3,#,#,9,#,#"WhyNegative values are serialized as normal tokens, and null children are still represented explicitly.

Constraints

  • The number of nodes is in the range [0, 10^4]
  • -1000 <= Node.val <= 1000
  • The tree may be empty
  • The tree may be highly unbalanced
  • deserialize must correctly reconstruct any string produced by serialize

Function Signature

def serialize_deserialize_tree(root):

Problem

Problem

In a Meta-style systems setting such as caching tree-structured ranking features for Facebook Feed, you may need to convert a binary tree into a compact string and later rebuild the exact same tree. Implement both serialization and deserialization.

Given the root of a binary tree, write two functions:

  1. serialize(root) that converts the tree into a string.
  2. deserialize(data) that reconstructs the original binary tree from that string.

Your encoding must preserve both node values and structure, including missing children.

Formal Specification

  • Input to serialize: root, the root node of a binary tree or null
  • Output from serialize: a string representation of the tree
  • Input to deserialize: data, a string produced by serialize
  • Output from deserialize: the reconstructed root node or null

You may choose any deterministic format, but deserialize(serialize(root)) must produce a tree identical to the original.

Examples

Example 1

  • Input: root = [1,2,3,null,null,4,5]
  • Output: "1,2,#,#,3,4,#,#,5,#,#"
  • Explanation: Preorder traversal with # markers preserves missing children.

Example 2

  • Input: root = []
  • Output: "#"
  • Explanation: An empty tree is represented by a single null marker.

Constraints

  • Number of nodes is in the range [0, 10^4]
  • -1000 <= Node.val <= 1000
  • The tree may be unbalanced
  • The output format must be lossless and deterministic

Examples

Example 1
Inputroot = [1,2,3,null,null,4,5]Output"1,2,#,#,3,4,#,#,5,#,#"WhyPreorder records each node and each missing child, so the exact original structure can be reconstructed.
Example 2
Inputroot = []Output"#"WhyAn empty tree has no nodes, so a single null marker is sufficient.
Example 3
Inputroot = [7,-3,null,null,9]Output"7,-3,#,#,9,#,#"WhyNegative values are serialized as normal tokens, and null children are still represented explicitly.

Constraints

  • The number of nodes is in the range [0, 10^4]
  • -1000 <= Node.val <= 1000
  • The tree may be empty
  • The tree may be highly unbalanced
  • deserialize must correctly reconstruct any string produced by serialize

Function Signature

def serialize_deserialize_tree(root):
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
MetaSerialize and Deserialize Binary TreeHardMetaSerialize and Deserialize Binary TreeHardMetaSerialize and Deserialize Binary TreeHard
Next question