7.0 Introduction
Here are the steps of our algorithm:
Start by pushing the
rootnode to the queue.Keep iterating until the queue is empty.
In each iteration, first count the elements in the queue (let’s call it
levelSize). We will have these many nodes in the current level.Next, remove
levelSizenodes from the queue and push theirvaluein an array to represent the current level.After removing each node from the queue, insert both of its children into the queue.
If the queue is not empty, repeat from step 3 for the next level.
Example 1:








Last updated
Was this helpful?