Diameter is equal to the path between the two farthest nodes leaf1 and leaf2
getDiameterPath ( leaf1 , leaf2 , - 1 , path ) ; let pathSize = path . length ; if ( pathSize % 2 == 1 ) { document . write ( path [ parseInt ( pathSize / 2 , 10 ) ] ) ; } else { document . write ( path [ parseInt ( pathSize / 2 , 10 ) ] + " " + path [ parseInt ( ( pathSize - 1 ) / 2 , 10 ) ] ) ; } }