Practice Count Subtrees With Max Distance Between Cities
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Count Subtrees With Max Distance Between Cities
Problem Statement
There are n cities numbered from 1 to n. You are given an array edges of size n-1, where edges[i] = [ui, vi] represents a bidirectional edge between cities ui and vi. There exists a unique path between each pair of cities. In other words, the cities form a tree. A subtree is a subset of cities where every city is reachable from every other city in the subset, where the path between each pair passes through only the cities from the subset. Two subtrees are different if there is a city in one subtree that is not present in the other. For each d from 1 to n-1, find the number of subtrees in which the maximum distance between any two cities in the subtree is equal to d. Return an array of size n-1 where the dth element (1-indexed) is the number of subtrees in which the maximum distance between any two cities is equal to d. Notice that the distance between the two cities is the number of edges in the path between them.
Source: Count Subtrees With Max Distance Between Cities on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
n = 4, edges = [[1,2],[2,3],[2,4]]
[3,4,0]
# Example 2
n = 2, edges = [[1,2]]
[1]
# Example 3
n = 3, edges = [[1,2],[2,3]]
[2,1]
Constraints
Related LeetCode Problems
Practice more problems similar to Count Subtrees With Max Distance Between Cities.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.