Domanda di colloquio di Meta

Implement the code to display the nodes of a binary tree in order [i.e. level by level].

Risposte di colloquio

Anonimo

20 nov 2012

You can simply do a BFS. But if you want to distinguish the levels — for instance you want to know when to write a \n for marking the end of a level — you can use two queues, one being the "parents" queue and the other one the "children" queue. Iterate over the elements in the parents queue. For each element, display and insert its children in the chidren queue. When the parents queue is empty, display \n, switch queues and continue.

1

Anonimo

17 nov 2012

The answer is shown on previous questions.