Let the distance from A to B be X, then the time from A to B is x/20, and the return time is x/30, so the total time is x/20+x/30, and the traveling distance is x+x=2x.
So the average speed is 2x/(x/20+x/30)=24, which means the average speed of going back and forth is 24 kilometers per hour.