Serialize and Deserialize a Binary Tree
Updated
Problem statement
You will write both halves of a binary-tree codec. The encoder turns a tree into a string, and the decoder turns a string back into a tree. Your function gets a tree root and a string data. Encode root and decode data separately. The two do not have to describe the same tree.
Use preorder encoding. For each node, write its value as a signed decimal number, then encode its left subtree, then its right subtree. Write # wherever a pointer is null. Put exactly one comma between tokens, with no spaces and no comma at the end.
Return two strings, in this order:
- The preorder encoding of
root, written exactly as described above. - The tree decoded from
data, written as compact level-order JSON. Level order lists the nodes level by level, left to right, and compact means no spaces. Keepnullfor missing nodes in the middle, but drop anynullvalues at the end. An empty decoded tree is[].
You can assume data is always a valid preorder encoding that follows every format rule here, including the rules for writing numbers.
Function
binaryTreeCodec(root: TreeNode, data: String) → String[]Examples
Example 1
root = [1,2,3,null,null,4,5]data = "8,#,9,#,#"return = ["1,2,#,#,3,4,#,#,5,#,#","[8,null,9]"]Encoding root writes each node, then its whole left subtree, then its right subtree. Every leaf is followed by two # marks for its missing children. Decoding data gives a top node with no left child and a single leaf as its right child. So its level-order list keeps a null before that leaf. The two trees are unrelated.
Example 2
root = []data = "#"return = ["#","[]"]root is empty, so its encoding is just #. Decoding data, which is only #, gives an empty tree, written as [].
Example 3
root = [-7,null,3,-1]data = "0,-2,#,#,0,#,#"return = ["-7,#,3,-1,#,#,#","[0,-2,0]"]In root, the top node has no left child, so a # comes right after its value. Its right child comes next, then that child's left child, then the remaining # marks. In data, the top node has two leaf children, and the right one repeats the top value. Negative values keep their minus sign, and each repeated value stays in its own place.
Constraints
- Each input tree has from
0through500nodes. - Every node value satisfies
-10^6 <= value <= 10^6. - Numbers are written in decimal with no leading zeros and no plus sign. Zero is written as
0. - Both trees are finite binary trees with no cycles. Decoding must keep every value and every left and right link.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Recover a Tree From Preorder TraversalGrammarly · PHONE SCREEN
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Find File Paths in a Directory TreeUber · PHONE SCREENUber interview questions
- Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
- Serialize and Deserialize a Binary TreeGoogle · ONSITE INTERVIEWGoogle interview questions
- Count Distinct Rooted-Tree GraftsThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Vertical Order Traversal of a Binary TreeBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Recover a Tree from Preorder Depth EncodingSuperhuman · PHONE SCREENSuperhuman interview questions
- Account Roles With Parent-User InheritanceStripe · PHONE SCREENStripe interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions