Design a method to serialize and deserialize a binary tree. There are no specific constraints on how the serialization and deserialization should be implemented; the key requirement is that the serialized binary tree must be convertible back into the original tree structure.
The binary tree is represented by a collection of TreeNode
s, where each node has optional left
and right
child nodes, which are also TreeNode
s.
A TreeNode
has the following interface:
interface TreeNode {val: number;left: TreeNode | null;right: TreeNode | null;}
serializeBinaryTree
root: TreeNode
: Root node of the tree. Examples display a level-order traversal of the treedeserializeBinaryTree
data: string
: String representation of the tree. The format is up to you to designInput: root = [5,3,8,1,4,6,9]Output: [5,3,8,1,4,6,9]Explanation: The tree structure remains unchanged after serialization and deserialization.
Input: root = [1,null,2]Output: [1,null,2]Explanation: The tree structure remains unchanged after serialization and deserialization.
Input: root = [2,1,7]Output: [2,1,7]Explanation: The tree structure remains unchanged after serialization and deserialization.
TreeNode.val
<= 1,000,000Design a method to serialize and deserialize a binary tree. There are no specific constraints on how the serialization and deserialization should be implemented; the key requirement is that the serialized binary tree must be convertible back into the original tree structure.
The binary tree is represented by a collection of TreeNode
s, where each node has optional left
and right
child nodes, which are also TreeNode
s.
A TreeNode
has the following interface:
interface TreeNode {val: number;left: TreeNode | null;right: TreeNode | null;}
serializeBinaryTree
root: TreeNode
: Root node of the tree. Examples display a level-order traversal of the treedeserializeBinaryTree
data: string
: String representation of the tree. The format is up to you to designInput: root = [5,3,8,1,4,6,9]Output: [5,3,8,1,4,6,9]Explanation: The tree structure remains unchanged after serialization and deserialization.
Input: root = [1,null,2]Output: [1,null,2]Explanation: The tree structure remains unchanged after serialization and deserialization.
Input: root = [2,1,7]Output: [2,1,7]Explanation: The tree structure remains unchanged after serialization and deserialization.
TreeNode.val
<= 1,000,000console.log()
语句将显示在此处。