Algorithms and Data Structures · Middle · Question 37 of 75

How do you traverse a binary tree level by level?

Explanation

Record queue.length at the start of each level and process exactly that many nodes, pushing their children. This gives the levels separately, which many problems need (right side view, average per level).

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 37

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz