For each node, you need to count color frequencies across its entire subtree (including the node itself and all descendants).
Naive DFS recomputes frequency counts from scratch for every node by iterating through the entire subtree. This gives time in the worst case (think of a chain). Too slow for , nodes.
Can you reuse work from children? If you have already computed frequency maps for the left child and right child, can you merge them efficiently to get the parent's map?
Think about how merging works and how to avoid recomputing everything.