思路:
1、序列化当前节点,序列化左、右子树,返回序列化表示
2、基于序列化表示,找出节点值、节点左子树、右子树的序列化表示形式
3、递归建立当前节点,左子树、右子树,然后拼接后返回当前节点
class Solution:
def Serialize(self, root):
# write code here
if not root:
return "{}"
left = self.Serialize(root.left)
right = self.Serialize(root.right)
val = root.val
return str({"v":val,"l":left,"r":right})
def Deserialize(self, s):
if s == "{}":
return
dic = eval(s)
val = dic['v']
left_s = dic['l']
right_s = dic['r']
left = self.Deserialize(left_s)
right = self.Deserialize(right_s)
root = TreeNode(val)
root.left = left
root.right = right
return root