Leetcode Problem 2049. Count Nodes With the Highest Score
2049. Count Nodes With the Highest Score
Leetcode Solutions
DFS Traversal to Calculate Subtree Sizes and Scores
Build an adjacency list to represent the tree structure from the parents array.
Define a recursive DFS function that calculates the size of each subtree.
During the DFS traversal, calculate the score for each node by multiplying the sizes of the left subtree, right subtree, and the remaining nodes in the tree.
Keep track of the maximum score encountered and the count of nodes with that score.
After the DFS traversal is complete, return the count of nodes with the maximum score.
Iterative Post-order Traversal to Calculate Subtree Sizes and Scores