2049. Count Nodes With the Highest Score
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:
    def countHighestScoreNodes(self, parents: List[int]) -> int:
        n = len(parents)
        tree = defaultdict(list)
        for i, p in enumerate(parents):
            if p != -1:
                tree[p].append(i)

        def dfs(node):
            size = 1
            score = 1
            for child in tree[node]:
                s = dfs(child)
                score *= s
                size += s
            self.scores[node] = score * (n - size if node != 0 else 1)
            return size

        self.scores = {}
        dfs(0)
        max_score = max(self.scores.values())
        return sum(1 for score in self.scores.values() if score == max_score)

```
['Python3']