Skip to main content

88. Serialize and Deserialize Binary Tree

hardAsked at Workday

Design serialize/deserialize for a binary tree. Workday uses this for protocol-design + recursion mastery — same shape as transmitting an org-chart snapshot across services without losing structure.

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

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2026-Q1)Workday SDE3 onsite — design + tree.

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] after round-trip

Example 2

Input
root = []
Output
[]

Approaches

1. Level-order with null markers

BFS, emit nulls for absent children. Deserialize by walking and consuming.

Time
O(n)
Space
O(n)
// LeetCode's default format — works but bookkeeping is tricky

Tradeoff: Workable but the BFS reconstruction needs index pointers.

2. Preorder DFS with null sentinels

Serialize: preorder DFS, emit '#' for null. Deserialize: tokenize, recursively consume.

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

Tradeoff: Preorder is the simplest reconstructable form — at any token you know whether it's a node or null. No level metadata needed.

Workday-specific tips

Workday accepts either format. Preorder with sentinels is shorter. Use shared cursor (let i) instead of slicing the token array — slicing is O(n) per call.

Common mistakes

  • Using inorder — not enough info to reconstruct uniquely.
  • Slicing tokens.slice(1) recursively — quadratic.
  • Forgetting to consume the null token — desync between serialize and deserialize.

Follow-up questions

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

  • Serialize/Deserialize N-ary Tree (LC 428).
  • Serialize/Deserialize BST (LC 449) — no null sentinels needed.
  • What if the tree has parent pointers?

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?

Preorder visits root first. Combined with null sentinels, every prefix is enough to reconstruct the subtree it represents.

Why use a shared cursor?

Avoids O(n) per call for slicing. The build function reads the next token and advances; the cursor is module-level state.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →