请实现两个函数,分别用来序列化和反序列化二叉树
1 class Solution: 2 def __init_(self): 3 self.SerializeStr = '' 4 5 def Serialize(self, root): 6 if root == None: 7 return '#' 8 return str(root.val) + ' ' + self.Serialize(root.left) + ' ' +self.Serialize(root.right) 9 # write code here 10 def Deserialize(self, s): 11 self.SerializeStr = s 12 return self.DeserializeStr() 13 14 def DeserializeStr(self): 15 if len(self.SerializeStr) == 0: 16 return None 17 idx = self.SerializeStr.find(' ') 18 node = self.SerializeStr if idx == -1 else self.SerializeStr[:idx] 19 self.SerializeStr = '' if idx == -1 else self.SerializeStr[idx+1:] 20 if node == '#': 21 return None 22 val = int(node) 23 t = TreeNode(val) 24 t.left = self.DeserializeStr() 25 t.right = self.DeserializeStr() 26 return t 27 # write code here