Serialize and Deserialize a Binary Tree
Updated
Problem statement
Design your own way to turn a binary tree into a string, and to rebuild the tree from that string.
In one function, do both steps. First write an encoder that turns root into a string in any text format you like. Then write a decoder that builds a new tree from that string alone: it must not look at root. Return the tree the decoder builds.
The returned tree must have exactly the same shape and the same node values as root. The empty tree must also come back as the empty tree. The checker sees only the returned tree, not your string, so the format is up to you.
Function
serializeAndDeserialize(root: TreeNode) → TreeNodeExamples
Example 1
root = [5,2,9,null,4,7]return = [5,2,9,null,4,7]The tree has root 5. Its left child 2 has only a right child 4. Its right child 9 has only a left child 7. One possible encoding writes the nodes in preorder (node, left subtree, right subtree) with # for an empty child: 5,2,#,4,#,#,9,7,#,#,#. Reading that string back rebuilds the same tree, so the returned tree is the same as the input.
Example 2
root = []return = []The empty tree can be encoded as #, and decoding gives the empty tree back. The result is the empty tree.
Example 3
root = [7,7,null,7]return = [7,7,null,7]Three nodes all hold 7: the root, its left child, and that child's left child, a chain going left. An encoding that kept only the values would be 7,7,7 and could not tell this chain from other shapes. With empty-child markers it is 7,7,7,#,#,#,#, which rebuilds exactly this tree.
Example 4
root = [-1,null,-2,null,-3]return = [-1,null,-2,null,-3]A chain going right with negative values. One possible encoding is -1,#,-2,#,-3,#,#. The minus signs and the comma separators must both survive the round trip, so the result is the same chain.
Constraints
- The tree has between
0and1000nodes. -1000 <= Node.val <= 1000, and values can repeat.- No path from the root down to a leaf has more than
500nodes.
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- ClayWorkspace Resource TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Hierarchical In-Memory File SystemClay · PHONE SCREEN
- Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
- N-Ary Tree BFS CodecGoogle · ONSITE INTERVIEWGoogle interview questions
- N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Serialize and Deserialize a Binary TreeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Serialize and Deserialize an N-Ary TreeUber · ONSITE INTERVIEWUber interview questions
- Recover a Tree From Preorder TraversalGrammarly · PHONE SCREEN
- Design Search Autocomplete SystemRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
Interview reports that mention this question
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)