Serialize and Deserialize a Binary Tree
Updated
Problem statement
Design a way to serialize a binary tree into a string and to deserialize that string back into the same tree. Use this exact format:
- Visit the nodes in preorder: a node, then its whole left subtree, then its whole right subtree.
- Write each node as its integer value and each empty child as
#. - Separate the tokens with single commas and no spaces.
For example, the empty tree is #, and a single node with value -6 is -6,#,#.
Both halves are checked in one call. You are given a tree root and a string data, which may describe a different tree. Return an array of two strings:
Examples
Example 1
root = [5,3,8,null,4]data = "5,3,#,4,#,#,8,#,#"return = ["5,3,#,4,#,#,8,#,#","[5,3,8,null,4]"]Preorder writes 5, then its left subtree: 3, an empty left child #, and 4 with two empty children (4,#,#). Then the right subtree 8,#,#. That gives 5,3,#,4,#,#,8,#,#.
Here data is the same string, so rebuilding it gives the same tree. Its level order is [5,3,8,null,4]: the null is the empty left child of 3, and the empty children of 4 and 8 at the end are dropped.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.
More Amazon Onsite Interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- 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
- Serialize and Deserialize a Binary TreeNVIDIA · PHONE SCREENNVIDIA interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Vertical Order Traversal of a Binary TreeBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Vertical Order Traversal of a Binary TreeTokopedia · ONSITE INTERVIEWTokopedia interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions