The invention belongs to the technical field of preparation of nano-functional materials, in particular to a method for preparing a nitrogen doped carbon nanotube three-dimensional composite materialby in-situ growth of a few layers of titanium carbide, immersing ternary layered Ti3AlC2 ceramic powder in hydrofluoric acid solution, heating and stirring, centrifugally cleaning with ultrapure waterand absolute ethanol, drying to obtain two-dimensional layered titanium carbide nano-powder, adding it into tetramethylammonium hydroxide solution, heating and stirring, centrifuging with deionized water to obtain a few layers of titanium carbide nano-sheet dispersion; Adding cobalt salt into a dispersion of a few layers of titanium carbide nano-sheets for reaction, adding dicyandiamide, heatingand stirring until dicyandiamide is completely dissolved, freezing, and freeze-drying to obtain precursor powder; Nitrogen-doped carbon nanotubes (CNTs) three-dimensional composites were prepared by in-situ growth of a few layers of titanium carbide after grinding the precursor powder and heat treatment. A three-dimensional composite material is prepared by a simple pyrolysis method using a few layers of titanium carbide as a carrier, cobalt as a catalyst, dicyandiamide as a carbon and nitrogen source, and the electrochemical performance of the few layers of titanium carbide can be improved.