← 返回 amazon 的题目列表Binary Tree Encoding and Decoding
类型:online_judge
Problem Statement
Given a binary tree, design and implement two functions: one to encode the tree to a string and another to decode the string back to the original binary tree.
Function Signature
class Codec:
def serialize(self, root: TreeNode) -> str:
class Codec:
def deserialize(self, data: str) -> TreeNode:
Input and Output Description
Input: serialize receives the root of a binary tree, and deserialize receives a string representation of a binary tree.
Output: serialize returns the string representation of the tree, deserialize returns the root of the binary tree.
Examples
Example 1:
Input:
[1,2,3,null,null,4,5]
Output:
'1,2,3,null,null,4,5'
Example 2:
Input:
[]
Output:
''
Constraints
The number of nodes in the tree is in the range [0, 104].
Tree node values are between -1000 and 1000.
Example
Input
[1,2,3,null,null,4,5]