'''
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
'''
from collections import defaultdict
def GetTreeDiameter(edges):
adj = defaultdict(list)
for a, b in edges:
adj[a].append(b)
adj[b].append(a)
#print(adj)
visitted = set()
def dfs(v):
visitted.add(v)
if v not in adj:
return 0, 0
max_edges = 0
child_0, child_1 = 0, 0
for x in adj[v]:
if x in visitted:
continue
cur = dfs(x)
max_edges = max(max_edges, cur[0])
if cur[1] + 1 > child_0:
child_0 = min(cur[1] + 1, child_1)
child_1 = max(cur[1] + 1, child_1)
return 1 + max_edges, child_0 + child_1
return dfs(0)[0]
print (GetTreeDiameter([[0,1],[0,2]]))
print (GetTreeDiameter([[0,1],[1,2],[2,3],[1,4],[4,5]]))