Problems > Height of the Tree

Height of the Tree

Difficulty: 1200 4000 ms 128 MB

bfs trees

A tree is rooted at node 1. Print its height — the number of edges on the longest root-to-leaf path.

Input: the first line has N; each of the next N−1 lines has an edge u v.

Output: the height (0 for a single node).

Constraints:

  • 1 ≤ N ≤ 105

Sample Tests

Input 1
5
1 2
1 3
3 4
4 5
Output 1
3
Input 2
1
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.