Graph Theory37 sections · 1633 units
Open in Course

Think First - Lomsat gelral

Merging subtree frequency c...

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 O(n2)O(n^2) time in the worst case (think of a chain). Too slow for 100100,000000 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.