Skip to main content

99. Serialize and Deserialize Binary Tree

hardAsked at Salesforce

Design an algorithm to serialize/deserialize a binary tree. Salesforce uses this as the canonical 'design your own format' problem — they use similar serialization in their metadata-export pipeline.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses tree serialization in metadata-export and sandbox refresh.
  • Blind (2025-12)Recurring on Salesforce L5+ onsites.

Problem

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment. Design an algorithm to serialize and deserialize a binary tree.

Constraints

  • The number of nodes in the tree is in the range [0, 10^4].
  • -1000 <= Node.val <= 1000

Examples

Example 1

Input
root = [1,2,3,null,null,4,5]
Output
[1,2,3,null,null,4,5]

Example 2

Input
root = []
Output
[]

Approaches

1. Level-order with null markers

BFS; emit nulls for missing children. Deserialize via queue.

Time
O(n)
Space
O(n)
// BFS-based serialization — works but more code than preorder DFS.

Tradeoff: Acceptable.

2. Preorder DFS with null markers

Serialize: preorder DFS, append val or 'null'. Deserialize: split, recursive consume.

Time
O(n)
Space
O(n)
function serialize(root) {
  const parts = [];
  function dfs(n) {
    if (!n) { parts.push('null'); return; }
    parts.push(n.val);
    dfs(n.left); dfs(n.right);
  }
  dfs(root);
  return parts.join(',');
}
function deserialize(data) {
  const tokens = data.split(',');
  let i = 0;
  function build() {
    if (tokens[i] === 'null') { i++; return null; }
    const node = { val: parseInt(tokens[i++]), left: null, right: null };
    node.left = build();
    node.right = build();
    return node;
  }
  return build();
}

Tradeoff: Concise. Preorder ensures we can deserialize without knowing the tree structure in advance. Salesforce's preferred answer.

Salesforce-specific tips

Salesforce uses tree serialization in metadata-export (exporting org-hierarchy and field structures) and sandbox refresh. They grade on choosing a clean format AND making serialize + deserialize symmetric. Bonus signal: discuss the trade-off between human-readability (newline-separated) and compactness (binary or base64).

Common mistakes

  • Forgetting null markers — can't reconstruct ambiguous tree shapes.
  • Using inorder/postorder alone — can't be deserialized uniquely without additional info.
  • Off-by-one in token consumption during deserialize.

Follow-up questions

An interviewer at Salesforce may pivot to one of these next:

  • Serialize and Deserialize BST (LC 449) — exploit BST property.
  • Encode and Decode N-ary Tree (LC 428).
  • Compress the serialization (e.g., omit trailing nulls).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why preorder?

Because the root is emitted first, so we can build the tree top-down during deserialization. Postorder works too but requires reading from the back.

Why explicit null markers?

Without them, an in/preorder sequence is ambiguous. The nulls encode the tree shape unambiguously.

Practice these live with InterviewChamp.AI

Drill Serialize and Deserialize Binary Tree and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →