<p>There is an <strong>undirected</strong> tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>

<p>A node is <strong>good</strong> if all the <span data-keyword="subtree">subtrees</span> rooted at its children have the same size.</p>

<p>Return the number of <strong>good</strong> nodes in the given tree.</p>

<p>A <strong>subtree</strong> of <code>treeName</code> is a tree consisting of a node in <code>treeName</code> and all of its descendants.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]</span></p>

<p><strong>Output:</strong> <span class="example-io">7</span></p>

<p><strong>Explanation:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2024/05/26/tree1.png" style="width: 360px; height: 158px;" />
<p>All of the nodes of the given tree are good.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">edges = [[0,1],[1,2],[2,3],[3,4],[0,5],[1,6],[2,7],[3,8]]</span></p>

<p><strong>Output:</strong> <span class="example-io">6</span></p>

<p><strong>Explanation:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2024/06/03/screenshot-2024-06-03-193552.png" style="width: 360px; height: 303px;" />
<p>There are 6 good nodes in the given tree. They are colored in the image above.</p>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">edges = [[0,1],[1,2],[1,3],[1,4],[0,5],[5,6],[6,7],[7,8],[0,9],[9,10],[9,12],[10,11]]</span></p>

<p><strong>Output:</strong> <span class="example-io">12</span></p>

<p><strong>Explanation:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2024/08/08/rob.jpg" style="width: 450px; height: 277px;" />
<p>All nodes except node 9 are good.</p>
</div>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>edges.length == n - 1</code></li>
	<li><code>edges[i].length == 2</code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
	<li>The input is generated such that <code>edges</code> represents a valid tree.</li>
</ul>

class Solution:
    def countGoodNodes(self, edges: List[List[int]]) -> int:
        n = len(edges) + 1
        adj = self.build_adj_list(edges, n)
        
        sub_size = self.get_subtree_sizes(n, adj)
        
        return self.calculate_good_nodes(n, adj, sub_size)
    
    def build_adj_list(self, edges: List[List[int]], n: int) -> List[List[int]]:
        adj = [[] for _ in range(n)]
        for u, v in edges:
            adj[u].append(v)
            adj[v].append(u)
        return adj
    
    def get_subtree_sizes(self, n: int, adj: List[List[int]]) -> List[int]:
        sub_size = [0] * n
        vis = [False] * n
        self.calc_subtree_sizes(0, adj, sub_size, vis)
        return sub_size
    
    def calc_subtree_sizes(self, node: int, adj: List[List[int]], sub_size: List[int], vis: List[bool]) -> int:
        vis[node] = True
        size = 1
        for neigh in adj[node]:
            if not vis[neigh]:
                size += self.calc_subtree_sizes(neigh, adj, sub_size, vis)
        sub_size[node] = size
        return size
    
    def calculate_good_nodes(self, n: int, adj: List[List[int]], sub_size: List[int]) -> int:
        vis = [False] * n
        return self.count_good(0, adj, sub_size, vis)
    
    def count_good(self, node: int, adj: List[List[int]], sub_size: List[int], vis: List[bool]) -> int:
        vis[node] = True
        child_sizes = self.collect_child_sizes(node, adj, sub_size, vis)
        good_count = self.calculate_child_good_nodes(node, adj, sub_size, vis)
        
        if len(child_sizes) <= 1:
            good_count += 1
        
        return good_count
    
    def collect_child_sizes(self, node: int, adj: List[List[int]], sub_size: List[int], vis: List[bool]) -> set:
        child_sizes = set()
        for neigh in adj[node]:
            if not vis[neigh] and sub_size[neigh] > 0:
                child_sizes.add(sub_size[neigh])
        return child_sizes
    
    def calculate_child_good_nodes(self, node: int, adj: List[List[int]], sub_size: List[int], vis: List[bool]) -> int:
        good_count = 0
        for neigh in adj[node]:
            if not vis[neigh]:
                good_count += self.count_good(neigh, adj, sub_size, vis)
        return good_count
114518