Pergunta de entrevista da empresa Meta

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

Respostas da entrevista

Sigiloso

20 de nov. de 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

Sigiloso

17 de nov. de 2012

The answer is shown on previous questions.