The near miss between two planes was avoided by the “narrowest margin”, investigators say.