Zigzag Level Order Traversal of Binary Tree
In this post, I’m going to tackle a medium difficulty problem called Binary Tree Zigzag Level Order Traversal. I believe this is an important technique to learn while doing a breadth first traversal of a tree. This approach is generic enough to be used with many other leetcode problems.
Breadth first search
Breadth first search (BFS) relies on a queue to keep track of the elements. So elements are processed in the order they were added (FIFO). In general, BFS algorithm uses the following approach:
Level order traversal
If we want to process a tree level-by-level, we need to slightly modify the algorithm as follows:
Here we are intentional about how many elements we process at a time. We process nodes that were already in the queue when the for loop started executing. This ensures we process elements level by level.
Zig zag level order traversal
Finally to process levels in zig-zag fashion, we process elements left to right for one level and right to left for the next level and so on. But this can be easily achieved by processing all levels left to right and simply reversing the elements before placing the vector in the results. We use a boolean flag to decide when to reverse and we keep toggling it after each level.