def tree_to_newick_rec(cur_node):
    """ This recursive function is a helper function to generate the Newick string of a tree. """

    items = []
    num_children = len(cur_node.descendants)
    for child_idx in range(num_children):
        s = ''
        sub_tree = tree_to_newick_rec(cur_node.descendants[child_idx])
        if sub_tree != '':
            s += '(' + sub_tree + ')'
        s += cur_node.descendants[child_idx].name
        items.append(s)
    return ','.join(items)