human
Find farthest node from each node in Tree | Add edge between U and V in tree.
Edge from U to V.
Edge from V to U.
DFS to find the first End Node of diameter.
Calculating level of nodes.
Go in opposite direction of parent.
Function to clear the levels of the nodes.
Set all value of lvl [ ] to 0 for next dfs.
Set maximum with 0.
DFS will calculate second end of the diameter.
Calculating level of nodes.
Store the node with maximum depth from end1.
Go in opposite direction of parent.
Function to find the distance of the farthest distant node.
Storing distance from end1 to node u.
Function to find the distance of nodes from second end of diameter.
Storing distance from end2 to node u.
Joining Edge between two nodes of the tree.
Find the one end of the diameter of tree.
Find the other end of the diameter of tree.
Find the distance to each node from end1.
Find the distance to each node from end2.
Comparing distance between the two ends of diameter.
Driver code.
Function Call"
How can the above be solved in Python?

What is a code to find farthest node from each node in Tree and the other steps required?
assistant
#Here is code to find farthest node from each node in Tree and the other steps required.
 def AddEdge ( u , v ) :
	global adj
	adj [ u ] . append ( v )
	adj [ v ] . append ( u )
def findFirstEnd ( u , p ) :
	global lvl , adj , end1 , maxi
	lvl [ u ] = 1 + lvl [ p ]
	if ( lvl [ u ] > maxi ) :
		maxi = lvl [ u ]
		end1 = u
	for i in range ( len ( adj [ u ] ) ) :
		if ( adj [ u ] [ i ] != p ) :
			findFirstEnd ( adj [ u ] [ i ] , u )
def clear ( n ) :
	global lvl , dist1 , dist2
	for i in range ( n + 1 ) :
		lvl [ i ] = 0
	maxi = 0
	dist1 [ 0 ] = dist2 [ 0 ] = - 1
def findSecondEnd ( u , p ) :
	global lvl , adj , maxi , end2
	lvl [ u ] = 1 + lvl [ p ]
	if ( lvl [ u ] > maxi ) :
		maxi = lvl [ u ]
		end2 = u
	for i in range ( len ( adj [ u ] ) ) :
		if ( adj [ u ] [ i ] != p ) :
			findSecondEnd ( adj [ u ] [ i ] , u )
def findDistancefromFirst ( u , p ) :
	global dist1 , adj
	dist1 [ u ] = 1 + dist1 [ p ]
	for i in range ( len ( adj [ u ] ) ) :
		if ( adj [ u ] [ i ] != p ) :
			findDistancefromFirst ( adj [ u ] [ i ] , u )
def findDistancefromSecond ( u , p ) :
	global dist2 , adj
	dist2 [ u ] = 1 + dist2 [ p ]
	for i in range ( len ( adj [ u ] ) ) :
		if ( adj [ u ] [ i ] != p ) :
			findDistancefromSecond ( adj [ u ] [ i ] , u )
def findNodes ( ) :
	global adj , lvl , dist1 , dist2 , end1 , end2 , maxi
	n = 5
	AddEdge ( 1 , 2 )
	AddEdge ( 1 , 3 )
	AddEdge ( 3 , 4 )
	AddEdge ( 3 , 5 )
	findFirstEnd ( 1 , 0 )
	clear ( n )
	findSecondEnd ( end1 , 0 )
	findDistancefromFirst ( end1 , 0 )
	findDistancefromSecond ( end2 , 0 )
	for i in range ( 1 , n + 1 ) :
		x = dist1 [ i ]
		y = dist2 [ i ]
		if ( x >= y ) :
			print ( end1 , end = " ▁ " )
		else :
			print ( end2 , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	adj = [ [ ] for i in range ( 10000 ) ]
	lvl = [ 0 for i in range ( 10000 ) ]
	dist1 = [ - 1 for i in range ( 10000 ) ]
	dist2 = [ - 1 for i in range ( 10000 ) ]
	end1 , end2 , maxi = 0 , 0 , 0
	findNodes ( )