Explaned well || fully commented || java || using HashMap || Creating tree
Intuition\n Describe your first thoughts on how to solve this problem. \nAs the parents value is given in the array where parents[i] is the parent of i. \nparen
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAs the parents value is given in the array where parents[i] is the parent of i. \nparents =[-1,3,1,0,3]\nIn this case parent of 3 has two child, i.e, 1 and 4 as 3 is coming twice in the array at index 1 and 4. Parent 1 has 2 as the child and 0 as 3 as the child. So, it will look something like this -->\n\n           0\n          /\n         3\n        / \\\n       1   4\n\nSo, When we can decipher the Tree, then we need to find the size if we disconnect the parent node and then one or more than one subtree would be formed. So, we then need to find the product of the size of the subtree after removing a node.\nAlso, the twist is the we don\'t need to return the max score, rather than that, we need to return the size of the nodes with maximum score.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n![Highest Score.png](https://assets.leetcode.com/users/images/29e260a5-b376-4c2e-aa5a-e01d8c4d4dc6_1716396859.7979002.png)\n\nFirstly, we will create a Tree using the parents array. So, we will create a Tree array and add the i index at every place.\nThen, we will find the node[parent[i]] and add the left or the right child of it with the node[i].\n\nThen, we will use another function to calculate scores where we will do the dfs post order traversal on the Tree which we made.\n\nMost important thing is to check the leftSize and rightSize and if they are not 0, then we will multiply it with the score.\n\n****totalNodes - leftSize - rightSize - 1 ==> this we will do to check the number of nodes if we remove one.\n\nThen, we will take a global variable maxScore and update the max score everytime in it.\n\nLast but not the least, we will add the score as the key and node.val as the value in the list in the HashMap and store it accordingly.\n\nP.s --> we need to use Long data type as we are multiplying and it can have Integer overflow. So, please use long.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\ncreating the tree will be O(n) as we are just going once to every node and adding it.\nCalculating scores will also be O(n) as we are storing the score with it value which are having the same score in the hashmap. So, we are avoiding the repeated counts in the recursion by storing it in the hashmap. So, it will be O(n).\nTherefore, time will be o(n);\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nAs the stack will take O(n) in the calculateScore function. Therefore, maximum will be O(n).\n\n# Code\n```\nclass Solution {\n\n    //using long as we are multiplying and it can have (32bits)Integer overflow. So, long(64bits) will have more memory to store.\n    HashMap<Long, List<Long>> map = new HashMap<>();\n    int totalNodes = 0;\n    long maxscore = 0;\n\n    public int countHighestScoreNodes(int[] parents) {\n        TreeNode root = createTree(parents);\n        totalNodes = parents.length;\n        calculateScores(root);\n        return map.get(maxscore).size();\n    }\n\n    public int calculateScores(TreeNode node){\n        if(node == null) return 0;\n\n        int leftSize = calculateScores(node.left);\n        int rightSize = calculateScores(node.right);\n\n        long score = 1;\n        if(leftSize != 0) score *= leftSize; //this is for the left side to multiply the value \n        if(rightSize != 0) score *= rightSize;//this is for the right side to multiply the value \n\n        //this tells us that if we remove parent node, then left side \n        if(totalNodes - leftSize - rightSize -1 != 0){\n            score *= (totalNodes - leftSize - rightSize -1); \n        }\n\n\n        maxscore = Math.max(score, maxscore);//to check the max out of the nodes.\n\n        map.putIfAbsent(score, new ArrayList<>());//with every score add the number of does in the list.\n        map.get(score).add((long)node.val);//add the val in it.\n\n        return leftSize + rightSize +1;\n    }\n\n    public TreeNode createTree(int[] parents){\n        int n = parents.length;\n        TreeNode[] nodes = new TreeNode[n]; \n\n        //will create a TreeNode of the index and use this index for the parents.\n        for(int i = 0; i<n;i++){\n            nodes[i] = new TreeNode(i);//[0,1,2,3,4]\n        }\n\n        TreeNode root = null;\n        //[-1, 2, 0,2,0]\n        for(int i = 0; i<n;i++){\n            \n            if(parents[i] == -1) root = nodes[i];//will add the 0 in the parent.\n            else{\n                TreeNode parent = nodes[parents[i]];//2 , 0 , 2, 0\n                if(parent.left == null){\n                    parent.left = nodes[i];//2-->1 , 0-->2\n                }else{\n                    parent.right = nodes[i];//2-->3, 0--> 4\n                }\n            }\n        }\n\n        return root;\n    }\n\n}\n```
['Java']