<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def reverse_tree(tree):
 """Reverse the dependency tree. ie. the keys of the resulting dict are objects of type ReqPackage and the values are lists of DistPackage objects. :param dict tree: the pkg dependency tree obtained by calling `construct_tree` function :returns: reversed tree :rtype: dict """

    rtree = defaultdict(list)
    child_keys = set(c.key for c in flatten(tree.values()))
    for k, vs in tree.items():
        for v in vs:
            node = find_tree_root(rtree, v.key) or v
            rtree[node].append(k.as_required_by(v))
        if k.key not in child_keys:
            rtree[k.as_requirement()] = []
    return rtree