2 Samuel 24:13-23 OTBDC: Did the loving God take the life of the child of David because of the sin of David and Bathsheba?

It was David and Bathsheba who sinned egregiously, why did their son have to die, since Deuteronomy 24:16 and Ezekiel 18:20 specifically tell us that a son is not to die for the error of his father?