LeetCode 1519: Number of Nodes in the Sub-Tree With the Same Label Solution
Master LeetCode problem 1519 (Number of Nodes in the Sub-Tree With the Same Label), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1519. Number of Nodes in the Sub-Tree With the Same Label
Problem Explanation
Explanation
To solve this problem, we can perform a depth-first search (DFS) starting from the root node (node 0) and keep track of the count of each label in the subtree of each node. We can store this information in a map for each node. Then, we can iterate through the nodes and calculate the answer for each node based on its label count and its parent's label count.
- Perform a DFS to calculate the label count in the subtree of each node.
- Iterate through the nodes to calculate the answer for each node based on its label count and its parent's label count.
Time Complexity: O(n)
Space Complexity: O(n)
Solution Code
class Solution {
public int[] countSubTrees(int n, int[][] edges, String labels) {
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int[] edge : edges) {
graph.computeIfAbsent(edge[0], k -> new ArrayList<>()).add(edge[1]);
graph.computeIfAbsent(edge[1], k -> new ArrayList<>()).add(edge[0]);
}
int[] ans = new int[n];
int[] count = new int[26]; // Count of each label in the subtree of a node
boolean[] visited = new boolean[n];
dfs(0, graph, labels, count, visited, ans);
return ans;
}
private void dfs(int node, Map<Integer, List<Integer>> graph, String labels, int[] count, boolean[] visited, int[] ans) {
visited[node] = true;
int label = labels.charAt(node) - 'a';
int[] temp = new int[26];
temp[label] = 1;
for (int child : graph.getOrDefault(node, new ArrayList<>())) {
if (!visited[child]) {
int[] childCount = new int[26];
dfs(child, graph, labels, childCount, visited, ans);
for (int i = 0; i < 26; i++) {
temp[i] += childCount[i];
}
}
}
ans[node] = temp[label];
count[label] += temp[label];
for (int i = 0; i < 26; i++) {
ans[node] -= Math.min(count[i], temp[i]);
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1519 (Number of Nodes in the Sub-Tree With the Same Label)?
This page provides optimized solutions for LeetCode problem 1519 (Number of Nodes in the Sub-Tree With the Same Label) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1519 (Number of Nodes in the Sub-Tree With the Same Label)?
The time complexity for LeetCode 1519 (Number of Nodes in the Sub-Tree With the Same Label) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1519 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1519 in Java, C++, or Python.