implement tree diameter algorithm