-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1245-TreeDiameter.cs
55 lines (47 loc) · 1.54 KB
/
1245-TreeDiameter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//-----------------------------------------------------------------------------
// Runtime: 196ms
// Memory Usage: 35 MB
// Link: https://leetcode.com/submissions/detail/371242921/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _1245_TreeDiameter
{
private int maxLength = 0;
public int TreeDiameter(int[][] edges)
{
var graph = new Dictionary<int, List<int>>();
int N = edges.Length;
for (int i = 0; i <= N; i++)
graph[i] = new List<int>();
foreach (var e in edges)
{
graph[e[0]].Add(e[1]);
graph[e[1]].Add(e[0]);
}
DFS(0, graph, new bool[N + 1]);
return maxLength;
}
private int DFS(int index, Dictionary<int, List<int>> graph, bool[] visited)
{
if (visited[index]) return 0;
visited[index] = true;
int first = 0, second = 0;
foreach (var neighbor in graph[index])
{
int length = DFS(neighbor, graph, visited);
if (length > first)
{
second = first;
first = length;
}
else if (length > second)
second = length;
}
maxLength = Math.Max(maxLength, first + second);
return Math.Max(first, second) + 1;
}
}
}