Problem
Serialization changes a data structure into a string. You can then store the string or send it to another computer. Deserialization changes the string back into the original data structure.
Design a way to serialize and deserialize a binary tree. In a binary tree, each node has at most two children: a left child and a right child.
Write the class Codec with two methods. serialize(root) takes the root of a tree and returns a string. deserialize(data) takes such a string and returns the root of a tree.
You choose the string format. The only rule is that deserialize must build the same tree again from the string that serialize returns. The test uses one Codec object to serialize and a different Codec object to deserialize.
The examples show each tree as a list in level order. This list gives the nodes row by row, from left to right. The value null marks a missing child. Each output is the tree that deserialize returns.
Example 1
- Input:
- root = [7,2,9,null,null,5,11]
- Output:
- [7,2,9,null,null,5,11]
- Explanation:
deserialize(serialize(root))gives a tree with the same shape and the same values.
Example 2
- Input:
- root = [-4,null,8,3]
- Output:
- [-4,null,8,3]
Example 3
- Input:
- root = []
- Output:
- []
- Explanation:
- The format must also work for an empty tree.
Constraints
- 0 <= number of nodes <= 10^4
- -1000 <= Node.val <= 1000
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Design an in-memory file system that supports mkdir, ls, addContentToFile, and readContentFromFile
Serialize and deserialize a binary tree
Design Netflix's video streaming service
Coding
- Serialize and Deserialize N-ary Tree
Coding · Breadth-First Search · Depth-First Search · String
- All Nodes Distance K in Binary Tree
Coding · Binary Tree · Breadth-First Search · Depth-First Search
- Amount of Time for Binary Tree to Be Infected
Coding · Binary Tree · Breadth-First Search · Depth-First Search
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 6 records
Amazon
Leetcode 297. Serialize and Deserialize Binary Tree
Reported Oct 1, 2025 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026
View sourceLinkedIn
Leetcode 297. Serialize and Deserialize Binary Tree
Reported Apr 25, 2026 · 1 source report
MANAGER
Source snapshot: Nov 12, 2026
View sourceMeta
Leetcode 297. Serialize and Deserialize Binary Tree
Reported Aug 1, 2025 · 2 source reports
MANAGER, STAFF
Source snapshot: Nov 12, 2026
View sourceMicrosoft
Leetcode 297. Serialize and Deserialize Binary Tree
Reported May 29, 2026 · 5 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026