Skip to content

Practice Count Valid Paths in a Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2867
LeetCode Problem

Count Valid Paths in a Tree

Problem Statement

The undirected tree has n nodes labeled 1 through n and edges [u,v]. Count paths between two different nodes that contain exactly one prime node label. A path follows tree edges without repeating nodes, and a path and its reverse count only once.

Source: Count Valid Paths in a Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 5, edges = [[1,2],[1,3],[2,4],[2,5]]
Output
4

# Example 2

Input
n = 6, edges = [[1,2],[1,3],[2,4],[3,5],[3,6]]
Output
6

Constraints

1 <= n <= 10^5
edges.length == n - 1
edges[i].length == 2
1 <= ui, vi <= n
The input is generated such that edges represent a valid tree.

Related LeetCode Problems

Practice more problems similar to Count Valid Paths in a Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.