Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
Example:
You may serialize the following tree:
1 2 3 4 5
1 / \ 2 3 / \ 4 5
as “[1,2,3,null,null,4,5]” Clarification: The above format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
Solution:
The idea is to use preorder traversal to generate the string and create the tree.
serialize Use stringstream to build the string. Traverse the Tree with preorder traversal Insert empty node as ‘#’, separate the nodes between ‘ ‘.
deserialize Since we use preorder to store the tree, use preorder to deserialize it. idea is to use a queue to store all the nodes in data. The istringstream is already a queue and can help to separate string
Store the tree as a string, use , to separate each node. Use # to mark it as empty node.
When deserialize, use a global variable to process the data. Because the right side of tree would pick up the rest after left side is processed.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ classCodec { public: //searialize //Use stringstream to build the string. Traverse the Tree with preorder traversal. insert empty node as '#', separate the nodes between ' ' //deserialize //idea is to use a queue to store all the nodes in data. The istringstream is already a queue and can help to separate string //still use preorder traversal stringserialize(TreeNode* root){ ostringstream out; serialize(root, out); return out.str(); }
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None
classCodec: defserialize(self, root): ifnot root: return"" queue = deque([root]) res = [] while queue: front = queue.popleft() if front: queue.append(front.left) queue.append(front.right) res.append(str(front.val) if front else"#") return','.join(res)
defdeserialize(self, data): ifnot data: returnNone nodes = data.split(',') root = TreeNode(int(nodes[0])) queue = deque([root]) index = 1 while queue: front = queue.popleft() if nodes[index] != '#': front.left = TreeNode(int(nodes[index])) queue.append(front.left) index += 1 if nodes[index] != '#': front.right = TreeNode(int(nodes[index])) queue.append(front.right) index += 1 return root