3249. Count the Number of Good Nodes
Description
There is an undirected tree with n
nodes labeled from 0
to n - 1
, and rooted at node 0
. You are given a 2D integer array edges
of length n - 1
, where edges[i] = [ai, bi]
indicates that there is an edge between nodes ai
and bi
in the tree.
A node is good if all the subtrees rooted at its children have the same size.
Return the number of good nodes in the given tree.
A subtree of treeName
is a tree consisting of a node in treeName
and all of its descendants.
Example 1:
Input: edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
Output: 7
Explanation:

All of the nodes of the given tree are good.
Example 2:
Input: edges = [[0,1],[1,2],[2,3],[3,4],[0,5],[1,6],[2,7],[3,8]]
Output: 6
Explanation:

There are 6 good nodes in the given tree. They are colored in the image above.
Example 3:
Constraints:
2 <= n <= 105
edges.length == n - 1
edges[i].length == 2
0 <= ai, bi < n
- The input is generated such that
edges
represents a valid tree.
Solutions
Solution 1: DFS
First, we construct the adjacency list \(\textit{g}\) of the tree based on the given edges \(\textit{edges}\), where \(\textit{g}[a]\) represents all the neighboring nodes of node \(a\).
Next, we design a function \(\textit{dfs}(a, \textit{fa})\) to calculate the number of nodes in the subtree rooted at node \(a\) and to accumulate the count of good nodes. Here, \(\textit{fa}\) represents the parent node of node \(a\).
The execution process of the function \(\textit{dfs}(a, \textit{fa})\) is as follows:
- Initialize variables \(\textit{pre} = -1\), \(\textit{cnt} = 1\), \(\textit{ok} = 1\), representing the number of nodes in a subtree of node \(a\), the total number of nodes in all subtrees of node \(a\), and whether node \(a\) is a good node, respectively.
- Traverse all neighboring nodes \(b\) of node \(a\). If \(b\) is not equal to \(\textit{fa}\), recursively call \(\textit{dfs}(b, a)\), with the return value being \(\textit{cur}\), and add \(\textit{cur}\) to \(\textit{cnt}\). If \(\textit{pre} < 0\), assign \(\textit{cur}\) to \(\textit{pre}\); otherwise, if \(\textit{pre}\) is not equal to \(\textit{cur}\), it means the number of nodes in different subtrees of node \(a\) is different, and set \(\textit{ok}\) to \(0\).
- Finally, add \(\textit{ok}\) to the answer and return \(\textit{cnt}\).
In the main function, we call \(\textit{dfs}(0, -1)\) and return the final answer.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Here, \(n\) represents the number of nodes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|