Due to less light scattering and a better signal-to-noise ratio in deep imaging, two-photon fluorescence microscopy (TPFM) has been widely used in biomedical photonics since its advent. However, optical aberrations degrade the performance of TPFM in terms of the signal intensity and the imaging depth and therefore restrict its application. Here, we introduce adaptive optics based on the genetic algorithm to detect the distorted wavefront of the excitation laser beam and then perform aberration correction to optimize the performance of TPFM. By using a spatial light modulator as the wavefront controller, the correction phase is obtained through a signal feedback loop and a process of natural selection. The experimental results show that the signal intensity and imaging depth of TPFM are improved after aberration correction. Finally, the method was applied to two-photon fluorescence lifetime imaging, which helps to improve the signal-to-noise ratio and the accuracy of lifetime analysis. Furthermore, the method can also be implemented in other experiments, such as three-photon microscopy, light-sheet microscopy, and super-resolution microscopy.