← 返回 citadel 的题目列表Diameter and Special Nodes in Tree
类型:online_judge
citadel
Given a tree with N nodes, a node is called special if it is an end-point of any diameter path in the tree. All nodes are numbered from 1 to N. The root is special if it is node 1, and nodes from 1 to N are on the diameter. The diameter length will be the tree length, defined as the number of edges in the longest path of the tree.
Function Description:
Write a function countSpecialNodes that takes a list of edges as input.
edges[i] = [a, b] represents an edge between nodes a and b in the tree.
Return value:
Return an integer count of all special nodes in the tree.
Input Format:
First line: an integer N, total nodes in the tree.
Next N-1 lines: a b denotes edges in the tree.
Output Format:
Output an integer representing the number of special nodes in the tree.
Constraints:
1 <= N <= 10^5
Sample Input:
10 1 2 1 3 2 4 2 5 3 6 3 7 4 8 5 9 6 10
Sample Output:
3
Explanation:
Node 1 is the root and part of the diameter, nodes 8 and 10 are diameter endpoints, making them special.
Example
Input
10
1 2
1 3
2 4
2 5
3 6
3 7
4 8
5 9
6 10