<commit_before><commit_msg>python/dcp: day 3<commit_after># This problem was asked by Google.
# Given the root to a binary tree, implement serialize(root), which serializes the tree into a string,
# and deserialize(s), which deserializes the string back into the tree.

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

def serialize(root):
    if root:
        string = '{0},'.format(root.val)
        string = string + serialize(root.left)
        string = string + serialize(root.right)
        return string
    else:
        return 'X,'
    

def deserialize(data):
    if len(data) < 1:
        return None
    queue = [x.strip() for x in data.split(',')]
    return helper(queue)

def helper(queue):
    if len(queue) < 1:
        return None
    val = queue.pop(0)
    if val == 'X':
        return None
    else:
        node = TreeNode(int(val))
        node.left = helper(queue)
        node.right = helper(queue)
        return node

data = '1,2,X,X,3,4,X,X,5,X,X,'
assert(data == serialize(deserialize('1,2,X,X,3,4,X,X,5,X,X,')))
{'fraction_non_alphanumeric': 0.13261648745519714, 'fraction_numerical': 0.013440860215053764, 'mean_word_length': 2.6032258064516127, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4786459', 'n_tokens_mistral': 375, 'n_tokens_neox': 372, 'n_words': 105}