Find Root of a Colored Tree Problem
Description
LeetCode Problem 4085.
You are given an undirected tree of n nodes labeled from 0 to n - 1, represented by a 2D integer array edges of length n - 1, where edges[i] = [u_i, v_i] indicates that there is an undirected edge between nodes u_i and v_i.
You are also given a string colors of length n, where colors[i] is one of 'R', 'B', or 'W', and denotes the color of node i.
A node chosen as root must satisfy both of the following conditions once the tree is oriented away from it:
- Binary-tree property: Every node has at most two children.
- Layered color pattern: All nodes at the same depth have the same color. Starting at depth 0, these colors follow one of the three repeating sequences:
R → B → W → R → B → W → ...B → W → R → B → W → R → ...W → R → B → W → R → B → ...
Return the index of any root that satisfies both conditions, or -1 if no such root exists.
A tree is a connected undirected graph with no cycles.
The depth of a node is the number of edges on the path from the root to that node. The root has depth 0. A child of a node is an adjacent node whose depth is one greater.
The degree of a node is the number of edges incident to it.
Example 1:
Input: edges = [[3,1],[3,2],[1,4],[2,0]], colors = "BRRWB"
Output: 3
Explanation:

Rooting at node 3 gives each node at most two children and every edge advances to the next color in the cycle (W → R → B), so return 3.
Example 2:
Input: edges = [[0,1],[1,2],[2,3]], colors = "RBRB"
Output: -1
Explanation:

No root satisfies the required color step on every edge, so return -1.
Constraints:
2 <= n <= 10^5edges.length == n - 1edges[i] = [u_i, v_i]0 <= u_i, v_i < ncolors.length == ncolors[i]is one of'R','B','W'.- The input graph is a tree and every node has degree at most three.
Solution
Approach: Check Every Node's Neighbors
Number the colors W = 0, R = 1, B = 2, so one step along the cycle W -> R -> B -> W adds 1 modulo 3. In a valid layout every edge joins a node to its parent, whose color is exactly one step before its own.
Look at each node u with color c and count its neighbors by color:
- A neighbor with the same color
ccan never be a parent or a child, so the answer is -1. - Only color
(c + 2) % 3(one step earlier) can be the parent, and a node has at most one parent, so two such neighbors mean -1. - A degree above 3 is also -1.
If no neighbor has the "parent" color and the degree is below 3, then every neighbor of u is a child, there are at most two of them, and u is a candidate root. Every edge gives exactly one endpoint a parent, so once all counts are at most 1 exactly one node has no parent. If that node has degree 3 it would need three children, so it is not a valid root and the answer stays -1.
Complexity: O(n) time and O(n) space for the adjacency lists.
class Solution:
def findColoredRoot(self, edges: List[List[int]], colors: str) -> int:
d = {"W": 0, "R": 1, "B": 2}
n = len(colors)
g = [[] for _ in range(n)]
for u, v in edges:
g[u].append(v)
g[v].append(u)
ans = -1
for u, vs in enumerate(g):
if len(vs) > 3:
return -1
cnt = [0] * 3
for v in vs:
cnt[d[colors[v]]] += 1
c = d[colors[u]]
if cnt[c] > 0 or cnt[(c + 2) % 3] > 1:
return -1
if len(vs) < 3 and cnt[(c + 2) % 3] == 0:
ans = u
return ans