Skip to content

Practice Count Number of Possible Root Nodes

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

Hard#2581
LeetCode Problem

Count Number of Possible Root Nodes

Problem Statement

The tree has n = edges.length + 1 nodes labeled 0 through n - 1, and each edges[i] = [a,b] is an undirected edge. Each guesses pair [u,v] claims u is the parent of its adjacent node v after choosing a root. Return how many choices of root make at least k guesses correct. Return 0 when none qualifies.

Source: Count Number of Possible Root Nodes on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3
Output
3

# Example 2

Input
edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1
Output
5

Constraints

edges.length == n - 1
2 <= n <= 10^5
1 <= guesses.length <= 10^5
0 <= ai, bi, uj, vj <= n - 1
ai != bi
uj != vj
edges represents a valid tree.
guesses[j] is an edge of the tree.
guesses is unique.
0 <= k <= guesses.length

Related LeetCode Problems

Practice more problems similar to Count Number of Possible Root Nodes.

Review your practice with PhantomCodeAI

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