Java Visitor Pattern

  • + 1 comment

    1) You definitely need to add the reverse edges too because the graph is an undirected graph (even though it's a little unclear from the problem statement). Not adding the reverse edges would mean you are not creating the intended graph so you will get wrong results.

    2) If you want to do grandChildren.isEmpty(), it might be the case that grandChildren is null, and that will give a null pointer exception. For this reason, we first make sure that grandChildren != null before using the dot (.) operator on it.

    HackerRank solutions.