code: public static <T extends GraphNode<T>, C extends Collection<T>> C collectAllNodes(T node, C collection) {
          // we don't recurse if the collecion already contains the node
          // this costs a bit of performance but prevents infinite recursion in the case of graph cycles
          checkArgNotNull(collection, "collection");
          if (node != null && !collection.contains(node)) {
              collection.add(node);
              for (T child : node.getChildren()) {
                  collectAllNodes(child, collection);
              }
          }
          return collection;
      }
docs: Collects all nodes from the graph reachable from the given node in the given collection.
  This method can properly deal with cycles in the graph.
  
  @param node       the root node
  @param collection the collection to collect into
  @return the same collection passed as a parameter