← 返回 microsoft 的题目列表Serialize and Deserialize Binary Tree
类型:online_judge
Problem: Serialize and Deserialize Binary Tree
Design an algorithm to serialize and deserialize a binary tree.
Serialization: encode a binary tree into a string.
Deserialization: decode the string back into the original binary tree.
You must guarantee that for any binary tree root, deserialize(serialize(root)) reconstructs a tree with exactly the same structure and values.
Node Definition
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
Input/Output Format for Testing
The test harness uses preorder serialization:
A null node is represented by #.
Tokens are separated by commas.
Input contains one line: a preorder serialization string.
Your program should deserialize it and then serialize it back.
Constraints
Number of nodes: 0 <= n <= 10^4
Node values are 32-bit signed integers
Example
Input:
1,2,#,#,3,4,#,#,5,#,#
Output:
1,2,#,#,3,4,#,#,5,#,#
Example
Input
#
Output
#