This approach utilizes a Depth First Search (DFS) to traverse each node of the tree. During traversal, we maintain a frequency array for each node to keep count of each label in its subtree. The recursion allows merging results from child nodes to calculate the current node's results.
The time complexity is O(n), where n is the number of nodes, because each node and edge is visited once during the DFS traversal. The space complexity is O(n), required for storing the graph representation and frequency arrays.
1from collections import defaultdict
2
3class Solution:
4 def countSubTrees(self, n, edges, labels):
5 tree = defaultdict(list)
6 for a, b in edges:
7 tree[a].append(b)
8 tree[b].append(a)
9
10 result = [0] * n
11 count = [[0] * 26 for _ in range(n)]
12
13 def dfs(node, parent):
14 label_index = ord(labels[node]) - ord('a')
15 count[node][label_index] = 1
16 for child in tree[node]:
17 if child != parent:
18 dfs(child, node)
19 for i in range(26):
20 count[node][i] += count[child][i]
21 result[node] = count[node][label_index]
22
23 dfs(0, -1)
24 return result
25
The Python solution constructs a tree using a defaultdict. The DFS traversal is used to keep a frequency array storing label counts for each node's subtree. As the function moves from child to parent nodes, it updates the current node's counts using its children's data. This is used to determine the result for each node.
This alternative approach utilizes a HashMap to dynamically manage counts of node labels as opposed to fixed-size arrays. The hash map structure allows potential extension to accommodate varying character sets, though for this problem, it's implemented for the fixed set of labels 'a' to 'z'.
The solution has a time complexity of O(n) given tree node analysis occurs once each during DFS with label counting operations. Space complexity is O(n), due to memory allocations for the graph and storage structures.
1#include <vector>
2#include <unordered_map>
3#include <string>
4using namespace std;
5
6class Solution {
7public:
8 vector<int> countSubTrees(int n, vector<vector<int>>& edges, string labels) {
9 vector<vector<int>> graph(n);
10 for (auto& edge : edges) {
11 graph[edge[0]].push_back(edge[1]);
12 graph[edge[1]].push_back(edge[0]);
13 }
14 vector<int> result(n, 0);
15 vector<unordered_map<char, int>> count(n);
16 dfs(0, -1, graph, labels, count, result);
17 return result;
18 }
19
20private:
21 void dfs(int node, int parent, vector<vector<int>>& graph, string& labels, vector<unordered_map<char, int>>& count, vector<int>& result) {
22 char label = labels[node];
23 count[node][label] = 1;
24 for (int child : graph[node]) {
25 if (child == parent) continue;
26 dfs(child, node, graph, labels, count, result);
27 for (auto& [key, value] : count[child]) {
28 count[node][key] += value;
29 }
30 }
31 result[node] = count[node][label];
32 }
33};
The C++ implementation uses a map to manage dynamic label counts. The DFS recursions traverse nodes and merge child data into parents, allowing flexible character count storage and calculations to deliver output with label occurrences per subtree rooted at the respective nodes.