Java Solution | Subnodes Count | With Explanation
Counts array is having the counts of subnodes for each node.\nNow removing a node , remaining can be easily get with product of subnodes of that node\'s subnode
Counts array is having the counts of subnodes for each node.\nNow removing a node , remaining can be easily get with product of subnodes of that node\'s subnodes and upper nodes (total - counts[node])\nIn case of parent we dont have any upper nodes so we dont do it when i==0.\nStore all products along with thier frequency in a HashMap then return the max one.\n```\nclass Solution {\n    public int countHighestScoreNodes(int[] parents) {\n        int n = parents.length;\n        List<Integer>[] tree = new ArrayList[n];\n        \n\t\tfor(int i=0;i<n;i++)\n            tree[i] = new ArrayList<>();\n       \n\t    for(int i=1;i<n;i++)\n            tree[parents[i]].add(i);\n       \n\t    int counts[] = new int[n];\n        Arrays.fill(counts,1);\n        \n\t\tdfs(0,tree,counts);\n        \n\t\tlong max = 0, res = 0;\n        HashMap<Long,Long> map = new HashMap<>();        \n        \n\t\tfor(int i=0;i<n;i++){\n            long prod = 1;\n            for(int u : tree[i]) {\n                prod *= counts[u];\n            }\n\t\t\t\n            if(i!=0) prod *= n-counts[i];\n            map.put(prod,map.getOrDefault(prod,0L)+1L);\n\t\t\t\n            if(prod >= max) {\n                max = prod;\n                res = map.get(prod);\n            }\n        }\n\t\t\n        return (int)res;\n    }\n\t\n    private int dfs(int node,List<Integer>[] tree,int counts[]){ \n\t\n        for(int next : tree[node])\n            counts[node] += dfs(next,tree,counts);\n    \n\t    return counts[node];\n\t\t\n    }\n}\n```
[]