private static <L> Tree<L> pruneNodesHelper(Tree<L> tree, Filter<L> filter) {
    if (filter.accept(tree.getLabel()))
      return null;
    List<Tree<L>> prunedChildren = new ArrayList<Tree<L>>();
    for (Tree<L> child : tree.getChildren()) {
      Tree<L> prunedChild = pruneNodesHelper(child, filter);
      if (prunedChild != null)
      prunedChildren.add(prunedChild);
    }
    if (prunedChildren.isEmpty() && ! tree.isLeaf())
      return null;
    return new Tree<L>(tree.getLabel(), prunedChildren);
  }