Transform flat list into (reverse) dependency tree.