2018-11-22 leetcode Serialize and Deserialize Binary Tree Serialize and Deserialize Binary TreeRecursive Preorder Version - 120ms 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Codec: def serialize(self, root): """Encodes a tree to a single string. :type root: TreeNode :rtype: str """ vals = [] def convert(node): if node: vals.append(str(node.val)) convert(node.left) convert(node.right) else: vals.append('#') convert(root) return ' '.join(vals) def deserialize(self, data): """Decodes your encoded data to tree. :type data: str :rtype: TreeNode """ def convert(): val = next(vals) if val == '#': return None node = TreeNode(int(val)) node.left = convert() node.right = convert() return node vals = iter(data.split()) return convert() # Your Codec object will be instantiated and called as such:# codec = Codec()# codec.deserialize(codec.serialize(root)) leet code Newer Longest Substring with At Least K Repeating Characters Older Construct Binary Tree from Preorder and Inorder Traversal