Problem Description
Given an undirected tree with n nodes (labeled from 0 to n - 1) and rooted at node 0, and provided with an array of edges, count the number of nodes that are "good". A node is considered good if the sizes of the subtrees rooted at each of its children are all identical. (A leaf node or a node with a single child is trivially good.) The goal is to return the total count of such good nodes.
Key Insights
- Compute the size of each subtree using Depth-First Search (DFS).
- For each node, gather the sizes of its children subtrees.
- A node with 0 or 1 child is always good since there is no discrepancy in subtree sizes.
- For nodes with two or more children, verify that all children subtrees have the same computed size.
- Use an undirected tree representation but avoid re-visiting the parent node during DFS.
Space and Time Complexity
Time Complexity: O(n) – Each node is visited once during the DFS. Space Complexity: O(n) – This includes the space for the recursion stack and the tree representation (adjacency list).
Solution
We approach the problem by first constructing an adjacency list from the given edges. Then, using DFS starting from the root (node 0), we compute the size of each subtree recursively. As we compute the size for a node, we maintain a list of the sizes of the subtrees for its children. After processing a node’s children, if there are at most one child, the node is automatically good. Otherwise, we check if all the values in that list are identical. If they are, we mark the node as good and increment our counter. Finally, we return the total count of good nodes.
Code Solutions
Below are implementations in Python, JavaScript, C++, and Java. Each solution uses DFS to compute subtree sizes and count the good nodes, with detailed in-line comments for clarity.