LeetCode 2867: Count Valid Paths in a Tree Solution
Master LeetCode problem 2867 (Count Valid Paths in a Tree), a hard 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.
2867. Count Valid Paths in a Tree
Problem Explanation
Explanation
To solve this problem, we can perform a depth-first search (DFS) on the tree starting from each node. During the DFS, we can keep track of the prime numbers encountered on the current path. If there is exactly one prime number on the path from the current node to any ancestor node, we increment the count of valid paths.
We can maintain an array of prime numbers to check if a number is prime efficiently. Additionally, we can use a hashmap to store the count of prime numbers encountered on the path from each node to its ancestors.
The time complexity of this solution is O(n) as we visit each node once during the DFS, and the space complexity is O(n) to store the prime numbers array and hashmap.
Solution Code
class Solution {
public int countValidPaths(int n, int[][] edges) {
List<Integer>[] tree = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) {
tree[i] = new ArrayList<>();
}
for (int[] edge : edges) {
tree[edge[0]].add(edge[1]);
tree[edge[1]].add(edge[0]);
}
int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; // prime numbers
Map<Integer, Integer>[] countMap = new HashMap[n + 1];
int[] result = new int[1];
dfs(tree, primes, countMap, 1, 0, result);
return result[0];
}
private void dfs(List<Integer>[] tree, int[] primes, Map<Integer, Integer>[] countMap, int node, int parent, int[] result) {
countMap[node] = new HashMap<>();
for (int prime : primes) {
countMap[node].put(prime, countMap.getOrDefault(parent, new HashMap<>()).getOrDefault(prime, 0));
}
for (int child : tree[node]) {
if (child == parent) continue;
for (int prime : primes) {
countMap[node].put(prime, countMap[node].get(prime) + countMap[child].getOrDefault(prime, 0));
}
}
for (int prime : primes) {
if (countMap[node].get(prime) == 1) {
result[0]++;
break;
}
}
for (int child : tree[node]) {
if (child == parent) continue;
dfs(tree, primes, countMap, child, node, result);
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2867 (Count Valid Paths in a Tree)?
This page provides optimized solutions for LeetCode problem 2867 (Count Valid Paths in a Tree) 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 2867 (Count Valid Paths in a Tree)?
The time complexity for LeetCode 2867 (Count Valid Paths in a Tree) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2867 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2867 in Java, C++, or Python.