Using recursion formula to calculate the values of lca [ ] [ ] and dist [ ] [ ]
lca [ node ] [ i ] = lca [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; dist [ node ] [ i ] = dist [ node ] [ i - 1 ] + dist [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; } } for ( let i = 0 ; i < graph [ node ] . length ; i ++ ) { if ( graph [ node ] [ i ] [ 0 ] == parent ) continue ; dfs ( graph [ node ] [ i ] [ 0 ] , node , h + 1 , graph [ node ] [ i ] [ 1 ] ) ; } }