# LeetCode: 449. 序列化和反序列化二叉搜索树¶

## 2、解题思路¶

​ 序列化的时候，我们可以选择使用前序遍历来做

​ 反序列化，同样的，根据前序遍历，我们使用递归来做

• 使用双向队列存储所有节点
• 因为是二叉搜索树，因此当前节点的左节点是小于当前节点的值，右节点是大于当前节点的值
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
from collections import deque
class Codec:

def serialize(self, root):
"""Encodes a tree to a single string.

:type root: TreeNode
:rtype: str
"""

ans = []

def pre_order(node):
if not node:
return
ans.append(node.val)
pre_order(node.left)
pre_order(node.right)

pre_order(root)
return ' '.join(map(str, ans))

def deserialize(self, data):
"""Decodes your encoded data to tree.

:type data: str
:rtype: TreeNode
"""
if not data:
return None
q = deque(map(int, data.split(" ")))

def dfs(minVal, maxVal):
if q and minVal < q[0] < maxVal:
val = q.popleft()
node = TreeNode(val)
node.left = dfs(minVal, val)
node.right = dfs(val, maxVal)
return node

return dfs(-sys.maxsize, sys.maxsize)

# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.deserialize(codec.serialize(root))