- Denisse's Newsletter
- Posts
- 🚀 Act like you already have the job and outwork everyone
🚀 Act like you already have the job and outwork everyone
Day 2 of master level by level trees


TLDR
Welcome!
Help your wellbeing with this free assessment from BetterHelp
Recommendations I live by!
Self-Gaslight of the week
JOBS & SCHOLARSHIPS
Hello and welcome back brave human being!
This is going to sound very creepy, very creepy indeed but this week I had a realization.
There is only death after this.
There is no escaping it. No matter how much we build, achieve, or run from it—it's the one constant in all of our lives.
So I ask you, how do you want to live your life? What dreams are you putting off?
The truth is that not working on your dreams is far more costly than failure. Time moves on whether we take action or not, and each day we let slip by without pursuing what truly matters is a day we’ll never get back.
LFG!!
LeetCode problem
Given the root of a binary tree, return the average value of the nodes on each level in the form of an array.
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, 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 by their levels as we go down the tree.
Once we have a map with the levels and nodes, we are going to find the average of each level simply by iterating through each level, summing the value of each node, and dividing it by the size of the array, resulting in the average value for that particular level.
Once the average is calculated, we add simply add it to our results array.

Complete code:
class Solution {
public:
map<int, vector<int>> nodes_by_level; // The key is the level, the value is the list of nodes at that level
// Helper function to recursively traverse the tree and group nodes by level
void auxAverageOfLevels(TreeNode* root, int level) {
if (root == NULL) {
return;
}
auxAverageOfLevels(root->left, level + 1); // Traverse left subtree
nodes_by_level[level].push_back(root->val); // Store the current node's value
auxAverageOfLevels(root->right, level + 1); // Traverse right subtree
}
// Main function to compute the average of each level
vector<double> averageOfLevels(TreeNode* root) {
vector<double> average_of_levels;
auxAverageOfLevels(root, 0); // Start traversal from the root at level 0
// Iterate over each level in the map
for (const auto& [level, nodes] : nodes_by_level) {
double sum_of_level = 0;
// Calculate the sum of nodes at the current level
for (int node_value : nodes) {
sum_of_level += node_value;
}
// Compute the average and add it to the result vector
average_of_levels.push_back(sum_of_level / nodes.size());
}
return average_of_levels;
}
}
Get your software delivered on time, within budget, achieving your goals
With ELEKS' product-oriented delivery, we ensure that your vision is realised with superior software that meets your strategic goals. We take full responsibility for your project's success and focus on maximising your product's business value.
Manage Stress with Expert Support – Start Therapy with BetterHelp
Feel like you’re spinning your wheels, juggling endless responsibilities? Work, school, relationships—everything adds up. BetterHelp makes finding the right therapist fast and easy with a 5-minute quiz that matches you with one of 32,000 licensed professionals. Connect with your therapist through phone, video, or text, so therapy fits your life. You can even use FSA or HSA dollars toward therapy. Join the 4 million people already finding support with BetterHelp. Sign up today and enjoy 30% off your first three months—reclaim your peace of mind in just 48 hours.
Hacks of the week
Want to get a job at a start up? Browse through Tech Week’s event, find all the VCs, and subscribe to their newsletter, they always send the best jobs.
Self-gaslight of the week
I truly believe you can gaslight yourself to success.
When in doubt, ask, what would the main character do?
Jobs and Scholarships
Cheers to you hacking your week,
Denisse
Reply