Implement flattening of dendrogram