Meta Interview Question

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

Interview Answers

Anonymous

Nov 20, 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

Anonymous

Nov 17, 2012

The answer is shown on previous questions.