Mariusz Najwer’s Post

Easter tree? 🌳 🪺 It’s very similar to the LeetCode 103 problem (Binary Tree Zigzag Level Order Traversal), but this time we don’t need to return an array of values. Instead, we need to mutate the tree and apply the changes directly to the nodes. So we traverse the tree level by level, collect all the nodes on each level, and then reverse the values on every odd level. We also have to remember that we don’t want to change the order of the nodes themselves, only their values. That’s why the built-in reverse() method isn’t enough here. We need a custom algorithm that swaps the values directly (Two pointers). #javascript #interview #node

  • No alternative text description for this image

The zigzag part trips people up because they confuse node order with value order. reverse() on the array breaks the structure. most solutions iterate twice per level—collect, then swap. single pass with two pointers from opposite ends cuts that in half

To view or add a comment, sign in

Explore content categories