Ezvor

3004. Maximum Subtree of the Same Color

MediumArray Preparing judge… LeetCode
ArrayDynamic ProgrammingTreeDepth-First Search

Hints

Hint 1
For each node, define a flag[v] indicating that the subtree of this node contains only one color or not.
Hint 2
In the DFS process, when you call dfs(u) from node v, after that DFS of u has finished, check if flag[u] == false, then flag[v] is also false.
Hint 3
Also if color[v] != color[u], flag[v] becomes false.