Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

You are given an undirected tree. Your task is to find the longest path in the tree.

The longest path in a tree is defined as the maximum number of edges between any two nodes. You are given a 2-dimensional array ‘Edges’ in which ‘Edges[i][0]’ and ‘Edges[i][1]’ contains an edge.

```
In the example below, the longest path is between nodes 1 and 3. The length of the path is 3 since the number of edges between node 1 and node 3 is 3. Hence, the answer is 3.
```

Detailed explanation

```
1 <= T <= 5
2 <= N <= 10^5
0 <= Edges[i][0], Edges[i][1] < N
Time limit: 1 sec
```

```
1
6
0 1
0 2
2 3
2 4
2 5
```

```
3
```

```
The longest path in the given tree is between nodes 1 and 3, and the length of the path is 3. Hence, the answer is 3.
```

```
1
12
0 1
0 2
0 3
1 5
1 6
2 7
3 4
3 9
7 8
9 10
9 11
```

```
6
```