The base problem (is this undirected graph a valid binary tree) I'd seen before, so I got through the structural check okay.
Model the problem as checking whether the graph can be partitioned into color-consistent levels via BFS from a candidate root, ensuring each level is monochromatic and edges only connect adjacent levels. Use the fact that in a valid tree, the root must be the unique node at distance 0, and all nodes at the same distance must share the same color. Validate by checking that the graph is connected, acyclic, and that BFS layers alternate colors consistently.
Pro tip: Clarify that the graph must be a tree (connected and acyclic) and that the root's color determines the color of all nodes at even distances, while the opposite color appears at odd distances. This reduces the problem to checking bipartiteness with respect to the root's color.
Restate the problem: given an undirected graph with colored nodes, determine if there exists a root such that the graph is a tree and each level (by distance from root) is monochromatic. Note that the graph must be connected and acyclic.
For a valid tree, the graph must be connected and have exactly n-1 edges. Also, the colors must alternate by level: all nodes at even distance from root have one color, and all at odd distance have the other. Thus, the graph must be bipartite with respect to the root's color.
Try each node as a potential root. For each root, perform BFS to assign levels and check that all nodes at the same level have the same color, and that no edges connect nodes at the same level or skip levels. Alternatively, use the fact that the root's color determines the color of all nodes at even/odd distances, and check if the graph is bipartite with that coloring.
Naively trying all roots takes O(n*(n+m)). Optimize by observing that the root must be a node whose color matches the majority color at even distances. Use BFS from any node to determine the two possible colorings, then check if either matches the given colors. This reduces to O(n+m).
Consider edge cases: single node (always valid), disconnected graph (invalid), cycles (invalid), and graphs where multiple roots could work. Validate the solution with examples and discuss trade-offs between different approaches.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.