Lungs of various amniotes, mapped onto the phylogeny of the respective organisms. The diagrams with question marks represent extinct groups, as reconstructed by Perry and his references. Mammals are very distant from birds and neither the mammalian diaphragm nor the alveolar lung is thought to be an ancestral character for the lineage leading to birds. The crocodile hepatic-piston method of ventilating the lungs (muscles pulling the liver backwards and thereby expanding the chest cavity) is not homologous to the mammalian diaphragm, and neither basal reptiles nor birds have diaphragms, so it is incorrect to claim that it is "almost certain" that dinosaurs had diaphragms. Perforations (holes) between lung chambers, however, are shared by birds and crocodiles, and thought to be ancestral, so the alleged "topological" problem in producing the bird flow-through lung is imaginary. Sauropods are known to have air sacs from fossil evidence, so air sacs were a feature of lungs in the dinosaurian ancestors of birds for tens of millions of years before theropod dinosaurs and then birds arose.
Phylogeny diagram by Nick Matzke. Lungs modified from Figure 1, p. 152 of: Perry, Steven F. (1992). "Gas exchange strategies in reptiles and the origin of the avian lung". Physiological Adaptations in Vertebrates. Wood, S. C., Weber, R. E., Hargens, A. R. and Millard, R. W., Eds. New York, Marcel Dekker: 149–167.