3. Consider the following C# code snippet to compute the diameter of a tree. What will be the output printed?
using System;
using System.Collections.Generic;
class Program {
static List<int>[] graph;
static int diameter = 0;
static int DFS(int node, int parent) {
int max1 = 0, max2 = 0;
foreach (var child in graph[node]) {
if (child == parent) continue;
int dist = DFS(child, node) + 1;
if (dist > max1) {
max2 = max1;
max1 = dist;
} else if (dist > max2) {
max2 = dist;
}
}
diameter = Math.Max(diameter, max1 + max2);
return max1;
}
static void Main() {
int n = 5;
graph = new List<int>[n];
for (int i = 0; i < n; i++) graph[i] = new List<int>();
graph[0].Add(1); graph[1].Add(0);
graph[1].Add(2); graph[2].Add(1);
graph[1].Add(3); graph[3].Add(1);
graph[3].Add(4); graph[4].Add(3);
DFS(0, -1);
Console.WriteLine(diameter);
}
}