The recently discovered second-order photonic topological insulators (SPTIs) are characterized by gapped edge states and robust corner states, and they provide novel approaches to the traditional ways to manipulate light. In a general case, the overlapped band gap of nontrivial and trivial photonic crystals composing SPTIs is narrow, which barely allows for the production of strongly localized states. Here, we introduce an intelligent numerical approach for the inverse design of large classes of SPTIs with great flexibility for controlling the properties of topological edge and corner states. In the optimized designs, the overlapped band gap of the nontrivial and trivial photonic crystals substantially exceeds that of the existing SPTIs, and it enables the existence of highly localized corner and edge states with nearly flat dispersion. We design several structures supporting both topological edge and corner states at the desired frequencies. Through programming newly created SPTIs, we suggest a strategy for routing topological edge and corner states. Our findings pave the way for the development of integrated photonic devices with topological protection and innovative functionalities.