def rename_tree(tree, names=None):
    """Rename the leaves of a tree from ints to names"""

    # rename leaves to integers
    for node in list(tree):
        if node.is_leaf():
            tree.rename(node.name, int(node.name))

    if names:
        # rename leaves according to given names
        for i, name in enumerate(names):
            tree.rename(i, name)

    return tree