Using recursion formula to calculate the values of lca [ ] [ ] , MinWeight [ ] [ ] and MaxWeight [ ] [ ]
lca [ node ] [ i ] = lca [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; NEW_LINE minWeight [ node ] [ i ] = min ( minWeight [ node ] [ i - 1 ] , minWeight [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ) ; NEW_LINE maxWeight [ node ] [ i ] = max ( maxWeight [ node ] [ i - 1 ] , maxWeight [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ) ; NEW_LINE for i in graph [ node ] : NEW_LINE if ( i == parent ) : NEW_LINE continue ; NEW_LINE dfs ( i , node , h + 1 ) ; NEW_LINE