class Solution:
def treeDiameter(self, edges: List[List[int]]) -> int:
n=len(edges)+1
graph=[[] for i in range(n)]
for u,v in edges:
graph[v].append(u)
graph[u].append(v)
diameter=0
def dfs(node,parent):
nonlocal diameter
max_1,max_2=0,0
for x in graph[node]:
if x==parent:continue
distance=dfs(x,node)+1
if distance> max_1:
max_1,max_2=distance,max_1
elif distance>max_2:
max_2=distance
diameter=max(diameter,max_1+max_2)
return max_1
dfs(0,-1)
return diameter
09-03
808
03-12
35
06-04
363
11-04
261
06-08
259
09-03
1061
07-11