Java Visitor Pattern

  • + 1 comment

    For my solution, the problem statement requires some clarification.

    1. A leaf node has only 1 edge.
    2. You are constructing an undirected graph.
    3. For an edge (u,v), most of the time u is the parent and v is the child, however, the order can be (v,u). That can happen when v is a child with a known depth and u has not had a depth assigned. -- This is the magic. Yes, a very unorthodox way to construct a graph.
    4. You may need to have multiple passes through the edges to construct the depth array.
    5. Use the depth array to determine if you call addChild.