Coding Round 2
Mid
programming
LeetCode #297 - Serialize and Deserialize Binary Tree
Design an algorithm to serialize and deserialize a binary tree. A binary tree can be represented as a string of values. Implement the Serialize and Deserialize functions that convert a binary tree into a single string and back to the original binary tree structure. For example, for the binary tree: [1,2,3,null,null,4,5], the serialized data should be a string representation like '1,2,3,null,null,4,5'. The goal is to capture the structure and values of the tree accurately.Input:
Output:
Example 1:
Example 2:
Constraints:
- A binary tree as a node structure containing
val,left, andright.
Output:
- A string representing the serialized binary tree.
Example 1:
- Input:
root = [1,2,3,null,null,4,5] - Output:
"1,2,3,null,null,4,5" - Explanation: This string represents the original binary tree's structure and values.
Example 2:
- Input:
root = [] - Output:
"" - Explanation: An empty tree should be serialized to an empty string.
Constraints:
- The tree nodes will have a maximum of 1000 nodes.
Suggested Answer