Microsoft interview question

Traverse a binary tree in a zig zag manner

Interview Answer

Anonymous

Feb 8, 2020

This question seem though at the beginning but I used the method I've been using before which was to first try to solve the question by hand. explaining my steps to the interviewer before coding. I drew a sample binary tree, then wrote down the values in the zigzag manner. While writing this down I noticed I needed to use two data structures. One to read from left to right and the other right to left. I first tried using two queues and this didn't work then I tried two stacks and it worked. When the interviewer was pleased with this explanation, I then implemented my algorithm in code