FastPrepSerialize and Deserialize a Binary Tree
Problems/Serialize and Deserialize a Binary Tree

Serialize and Deserialize a Binary Tree

Microsoft logoMicrosoft● HardINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A review of a Microsoft internship process lists, as one of its interview questions, turning a binary tree into a string and rebuilding it.

The report gives no string format, call shape or limits. Here the format is fixed so answers can be checked: preorder, # for an empty child, commas between tokens. You get a tree and a separate string in one call and return two strings. One is the tree's encoding. The other is the tree rebuilt from the string, written in level order with null for empty children and the trailing nulls removed (an empty tree is []). Both trees have at most 1000 nodes and are at most 500 levels tall.

Our version is about 80% the same as the reported question.

Design a way to turn a binary tree into a string (serialize) and to rebuild the tree from that string (deserialize). Use this format:

  • List the tree in preorder: the node, then its left subtree, then its right subtree.
  • Write a node as its value and an empty child as #.
  • Put one comma between tokens, with no spaces.

So the empty tree is #, and a single node with value 4 is 4,#,#.

You get two separate inputs, which may describe different trees. Return an array of two strings:

  1. the serialization of the tree root, and
  2. the tree rebuilt from the string data, written in level order as [v1,v2,...]: list the nodes level by level from left to right, write null for an empty child of a node, and remove all null values at the end. The empty tree is [].

Function

serializeAndDeserialize(root: TreeNode, data: String) → String[]

Examples

Example 1

root = [1,2,3,null,null,4,5]data = "1,2,#,#,3,4,#,#,5,#,#"return = ["1,2,#,#,3,4,#,#,5,#,#","[1,2,3,null,null,4,5]"]
root
12345

Preorder visits 1, then its left subtree (2, whose two children are empty, written #,#), then its right subtree (3, then 4 with two empty children, then 5 with two empty children). That gives 1,2,#,#,3,4,#,#,5,#,#. Here data describes the same tree, so rebuilding it and writing it in level order gives [1,2,3,null,null,4,5].

Example 2

root = []data = "7,#,-3,#,#"return = ["#","[7,null,-3]"]
root
empty

The empty tree is written as a single #. The string data describes a different tree: 7 with an empty left child and a right child -3 that has two empty children. In level order that is [7,null,-3]; the two empty children of -3 come last, so they are removed.

Constraints

  • root has from 0 to 1000 nodes.
  • Every node value in root is between -1000 and 1000.
  • 1 <= data.length <= 10^4
  • data is a valid encoding, in the format above, of a tree with 0 to 1000 nodes and values between -1000 and 1000. Values have no plus sign and no extra leading zeros.
  • Both trees (root and the tree that data describes) are at most 500 levels tall: no path from the root down to a leaf has more than 500 nodes.

More Microsoft Onsite Interview questions

  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
  • Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Serialize and Deserialize an N-Ary TreeUber · ONSITE INTERVIEWUber interview questions
  • N-Ary Tree BFS CodecGoogle · ONSITE INTERVIEWGoogle interview questions
  • Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
  • N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
  • JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
  • Word Dictionary with Wildcard FindAmazon · ONSITE INTERVIEW
  • Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
  • Nested Todo ListNotion · PHONE SCREEN · ONSITE INTERVIEWNotion interview questions

Interview reports that mention this question

  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
  • Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public String[] serializeAndDeserialize(TreeNode root, String data) {
        // Write your code here.
    }
}
Checking account…