ans
of size n
to store the result for each node.dfs
that takes the current node and its parent as arguments.dfs
, initialize a 26-length array count
to keep track of the label counts in the subtree.count[labels[node] - 'a']
to 1 since the node itself should be counted.dfs
on the child.count
array.ans[node]
to the count of the node's label.count
array from the dfs
function.dfs
starting from the root node (0) with no parent (-1).ans
array.