code: private static Map<String, Set<String>> flatten(Map<String, Set<String>> data) {
      Map<String, Set<String>> territories = new TreeMap<>();
      Set<String> keys = data.keySet();
      for (String key : keys) {
        Set<String> values = flatten(key, territories, data);
        territories.put(key, values);
      }
      return territories;
    }
docs: Flatten the territory containment hierarchy.