- Denisse's Newsletter
- Posts
- 💪 Turn Struggles into Strength
💪 Turn Struggles into Strength
Day 4 : 103. Binary Tree Zigzag Level Order Traversal
If you're frustrated by one-sided reporting, our 5-minute newsletter is the missing piece. We sift through 100+ sources to bring you comprehensive, unbiased news—free from political agendas. Stay informed with factual coverage on the topics that matter.
TLDR
Welcome!
Zig Zag Level Order
Read Unbiased news , The Internet File API
Interact with our sponsors to keep this newsletter free 💕
Recommendations I live by!
Self-Gaslight of the week
JOBS & SCHOLARSHIPS
Hello and welcome back brave human being!
Last week I had the pleasure to be at SFTechWeek and my biggest take away came from a book I bought at the airport.
It comes from an essay on resilience, How Resilience Works, and the author mentions a book I’ve tried to read plenty of times but have always avoided: Man Searching for Meaning by Victor Frankl.
He realized that to survive, he had to find some purpose. Frankl did so by imagining himself giving a lecture after the war on the psychology of the concentration camp, to help outsiders understand what he had been through.
Finding a reason to keep going can turn even the toughest situations into something meaningful.
LFG!!
LeetCode problem
Given the root of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).
Before we dive in,
Breathe
Nothing is ever that deep.
Now, let’s
But there is a little trick that I am about to teach you….
We’re going to leverage recursion to navigate through the tree and a map to store the nodes level by level, where the key is the level and the value is an array with the nodes in that level.
We are simply going to iterate through the tree INORDER, and store in our map the current node we are processing and their level:
treeNodes[level].push_back(root->val);
And every time we call our recursive function, we’re simply going to add 1 to our level so that when we traverse deeper into the tree, we know exactly where we are:
auxLevelOrder(root->left, level + 1);
auxLevelOrder(root->right, level + 1);
This approach allows us to group nodes together INORDER by their levels as we go down the tree.
Once we build our map of levels and their respective nodes, we loop through each level, reversing the nodes on odd levels to achieve the zigzag pattern.
Step by Step solution!

Complete Solution :
class Solution {
public:
map<int, vector<int>>nodes_by_level;
void auxZigzagLevelOrder(TreeNode* root, int level){
if (root==NULL){
return;
}
auxZigzagLevelOrder(root->left, level+1);
nodes_by_level[level].push_back(root->val);
auxZigzagLevelOrder(root->right, level+1);
}
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>>nodes_by_zigzag;
auxZigzagLevelOrder(root, 0);
for(const auto&[key,val]: nodes_by_level){
if(key%2==0){
nodes_by_zigzag.push_back(val);
}
else{
vector<int> temp = val;
reverse(temp.begin(), temp.end());
nodes_by_zigzag.push_back(temp);
}
}
return nodes_by_zigzag;
}
Recommendations
Level up your inbox by subscribing to what I am subscribed to!
Hacks of the week
Have you ever thought about hiring someone to apply to jobs for you? 👀
Self-gaslight of the week
I truly believe you can gaslight yourself to success.
Jobs and Scholarships
Cheers to you hacking your week,
Denisse
Reply