'Return the tree (as a list of edges) of the given Prufer sequence.
Examples
>>> from sympy.combinatorics.prufer import Prufer
>>> a = Prufer([0, 2], 4)
>>> a.tree_repr
[[0, 1], [0, 2], [2, 3]]
>>> Prufer.to_tree([0, 2])
[[0, 1], [0, 2], [2, 3]]
References
- https://hamberg.no/erlend/posts/2010-11-06-prufer-sequence-compact-tree-representation.html
See Also
tree_repr: returns tree representation of a Prufer object.'

@staticmethod
def to_tree(prufer):

    tree = []
    last = []
    n = (len(prufer) + 2)
    d = defaultdict((lambda : 1))
    for p in prufer:
        d[p] += 1
    for i in prufer:
        for j in range(n):
            if (d[j] == 1):
                break
        d[i] -= 1
        d[j] -= 1
        tree.append(sorted([i, j]))
    last = ([i for i in range(n) if (d[i] == 1)] or [0, 1])
    tree.append(last)
    return tree
