Imagine a family tree where node 0 is the patriarch/matriarch at the root. Each node represents a family member, and the connections show parent-child relationships. Given an undirected tree with n nodes labeled from 0 to n-1, you need to determine how many nodes are "good".
A node is considered good if all of its children's subtree sizes are identical. In other words, if you look at any node and count how many descendants each of its children has (including the child itself), those counts must all be equal for the node to be "good".
Input: A 2D array edges where edges[i] = [a_i, b_i] represents an edge between nodes a_i and b_i.
Output: Return the total count of good nodes in the tree.
Note: Leaf nodes (nodes with no children) are always considered good since there are no subtrees to compare!
Input & Output
Constraints
- 1 โค n โค 105 where n is the number of nodes
- edges.length == n - 1 (valid tree structure)
- 0 โค ai, bi < n
- The given edges form a valid tree (connected and acyclic)