Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.
Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'.
Pre-order traverse and use queue to store nodes
Use "#" as null
Use "," as spliter
defserialize(self,root): s =''if root isNone: s +='#'+','else: s +=str(root.val)+',' s += self.serialize(root.left) s += self.serialize(root.right)return sdefdeserialize(self,data): q = data.split(',')return self.dHelper(q)defdHelper(self,q): val = q.pop(0)if val =='#':returnNoneelse: node =TreeNode(val) node.left = self.dHelper(q) node.right = self.dHelper(q)return node