Clean Python code with easy to understand explanation.
Intuition\n Describe your first thoughts on how to solve this problem. \nThree Cases\nRoot node\nLeaf node\nMid node (neither noot nor leaf node)\nn = length of
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThree Cases\nRoot node\nLeaf node\nMid node (neither noot nor leaf node)\nn = length of parents array\n1. Leaf node leaves behind (n - 1) nodes\n2. Root node leaves behind nodes in left subtree and right subtree\n3. Mid node leaves behind nodes in left subtree and right subtree and n - (nodes in subtree with mid node as local root)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Create a graph dictionary with key = node integer value and val = list of children integer values\n2. DFS to find number of nodes in subtree of each node -> store in a dictionary\n3. Use the insight in the intuition section to calculate each node\'s score\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def countHighestScoreNodes(self, parents: List[int]) -> int:\n        \n        n = len(parents)\n        graph = defaultdict(list)\n        for node, parent in enumerate(parents):\n            graph[parent].append(node)\n        \n        def dfs(node):\n            visited.add(node)\n            res = 1\n            for nei in graph[node]:\n                if nei not in visited:\n                    res += dfs(nei)\n            nodes_in_subtree[node] = res\n            return res\n        \n        nodes_in_subtree = defaultdict(int)\n        visited = set()\n        dfs(0)\n\n        score_to_num_nodes = defaultdict(int)\n\n        for node in range(n):\n            if len(graph[node]) == 0:\n                score_to_num_nodes[n - 1] += 1\n                \n            elif node == 0:\n                score = 1\n                for child in graph[node]:\n                    score *= nodes_in_subtree[child]\n                score_to_num_nodes[score] += 1\n            \n            else:\n                score = 1\n                for child in graph[node]:\n                    score *= nodes_in_subtree[child]\n                score *= (n - nodes_in_subtree[node])\n                score_to_num_nodes[score] += 1\n        \n        max_score = max(score_to_num_nodes.keys())\n        return score_to_num_nodes[max_score]
['Depth-First Search', 'Recursion', 'Binary Tree', 'Python3']