[JAVA] Simple DFS Solution generalised for any tree with detailed comments. T=O(V+E), S=O(V+E)
\n/*\n    This solution is generalised to perform for any tree, not only binary tree. \n    To calculate score for each node, we need the number of nodes in the
```\n/*\n    This solution is generalised to perform for any tree, not only binary tree. \n    To calculate score for each node, we need the number of nodes in the subtrees whose root is a child of the current node \n    and also the number of nodes in the remaining tree excluding the subtree with root at the current node.\n    Once we have these values we find the product of the non-zero values among them and update the maximum score \n    and the count of the number of nodes with the maximum score.\n*/\nclass Solution {\n    long maxScore; // stores the maximum score\n    int count; // store the count of maximum score\n    public int countHighestScoreNodes(int[] parent) {\n        int n=parent.length;\n        \n        // creating the tree\n        List<Integer> list[]=new ArrayList[n];\n        for(int i=0;i<n;i++)\n            list[i]=new ArrayList<>();\n        for(int i=1;i<n;i++){\n            list[parent[i]].add(i);\n        }\n        \n        maxScore=0l;\n        count=0;\n        \n        dfs(0,list,n);\n        \n        return count;\n    }\n    \n    // returns the number of nodes in the subtree with root u.\n    public int dfs(int u, List<Integer> list[], int n){        \n        \n        int total=0; // stores total number of nodes in the subtree with root u, excluding u.\n        long prod=1l,rem,val;\n        \n        // traversing children in the subtree\n        for(int v:list[u]){\n            val=dfs(v,list,n); // number of nodes in the subtree with root v.\n            total+=val;\n            prod*=val;\n        }\n        rem=(long)(n-total-1); // number of nodes in the remaining tree excluding the subtree with root u.\n        \n        if(rem>0)// only count the remaining part if there is at least one node.\n            prod*=rem;\n        \n        // updating maxScore and count.\n        if(prod>maxScore){\n            maxScore=prod;\n            count=1;            \n        }\n        else if(prod==maxScore){\n            count++;\n        }\n        \n        return total+1; // returning total number of nodes in the subtree with node u.\n    }\n}\n```\nT = O(V+E)\nS = O(V+E)\nPlease upvote if you find this helpful.
[]