One level at a time
transcript
A tree has levels. The top is level one. A report needs one line for each level. One queue is enough.
Read the size of the queue first. Take exactly that many nodes, and no more. Push their children for the next round.
Round one: the queue holds only the top. Round two: the queue holds the two children. Round three: their two children follow. The levels never run together.
Read the size before you push anything into the queue. Children pushed during the round must wait for the next round. Read it late, and the new children join the round. The levels run together.
Keep a small summary for each level. It is a few numbers. A total and a count give an average. A best so far gives the maximum. Reset it every round.
Read the queue size first. Reset the summary each round. Full breakdown in the lecture.