Twitch Interview Question: Print a binary tree row-by-ro... |

Interview Question

Senior Software Engineer Interview Seattle, WA (US)

Print a binary tree row-by-row, printing left-to-right for

  even numbered rows and right-to-left for odd numbered rows.

Interview Answer

2 Answers


Implemented using Breadth-First-Search and keeping track of the current level, printing once you had 2^L items queued up.

Interview Candidate on 6 Jun 2017

Assuming no data structure restrictions, one possible approach is to utilise both a stack and a queue. Load LTR rows into a queue, then clear the queue by loading child nodes for RTL reading into the stack, and so on. Print a newline every time the stack or queue is exhausted, and terminate when both are exhausted.

JB on 1 Mar 2020

Add Answers or Comments

To comment on this, Sign In or Sign Up.