C++ DFS; Will work for n-ary tree also; Separate score and size count;
I separate the computation of the score for each node from the computation of the size of the node. This code will also work for a N-ary tree.\n\n\nclass Soluti
I separate the computation of the score for each node from the computation of the size of the node. This code will also work for a N-ary tree.\n\n```\nclass Solution {\npublic:\n    int dfs(int u, vector<vector<int>>& graph,vector<int>& numNodes) {\n        numNodes[u] = 1;\n        for (int v : graph[u]) {\n            numNodes[u] += dfs(v,graph,numNodes);\n        }\n        return numNodes[u];\n    }\n    int countHighestScoreNodes(vector<int>& parents) {\n        int n = parents.size();\n        \n        vector<vector<int>> graph(n);\n        for (int i = 1; i < n; i++) {\n            graph[parents[i]].push_back(i);\n        }\n        \n        vector<int> numNodes(n,-1);\n        dfs(0,graph,numNodes);\n        \n        vector<long long> score(n,0L);\n        long long maxScore = LLONG_MIN;\n        for (int i = 0; i < n; i++) {\n            score[i] = (i > 0) ? (numNodes[0] - numNodes[i]) : 1;\n            for (int child : graph[i]) {\n                score[i] *= numNodes[child];\n            }\n            maxScore = max(maxScore,score[i]);\n        }\n        \n        return count_if(begin(score),end(score),[maxScore](long long x){return x == maxScore;});\n    }\n};\n```
['Depth-First Search', 'C']