💔You can do it with a broken heart 💔

Day 1 of master level by level trees

In partnership with

In partnership with

Receive Honest News Today

Join over 4 million Americans who start their day with 1440 – your daily digest for unbiased, fact-centric news. From politics to sports, we cover it all by analyzing over 100 sources. Our concise, 5-minute read lands in your inbox each morning at no cost. Experience news without the noise; let 1440 help you make up your own mind. Sign up now and invite your friends and family to be part of the informed.

TLDR

  1. Welcome!

  2. Facts. Without motives. In 1 daily news briefing.

  3. Recommendations I live by!

  4. Self-Gaslight of the week

  5. JOBS & SCHOLARSHIPS

Hello and welcome back brave human being!

Let’s face it, rejection hurts, otherwise, we wouldn’t be so scared.

Today I attempted to do man-in-the-street interviews, spoiler it didn’t go great.

And let’s face it, rejection hurts.

But let me tell you the secret to life….

But here's the thing about life: success isn't about who works the hardest. It’s about who can handle the most failure.

So the next time you stumble, don’t think of it as failure. Think of it as one more rep, one step closer to your goal.

LFG!!

LeetCode problem

Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

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. Here’s the complete solution so you can see how it all comes together:

Complete code:

class Solution {
public:
    map<int, vector<int>> treeNodes; // The key is the level, the value is a vector of node values at that level

    void auxLevelOrder(TreeNode* root, int level) {
        if (root != NULL) {
            // Store the current node's value at its corresponding level
            treeNodes[level].push_back(root->val);
            
            // Recurse into the left subtree
            auxLevelOrder(root->left, level + 1);

            // Recurse into the right subtree
            auxLevelOrder(root->right, level + 1);
        }
    }

    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> answer;
        
        // Start the recursive level order traversal from the root at level 0
        auxLevelOrder(root, 0);
        
        // For each level in the map, add its corresponding node values to the final answer
        for (auto level : treeNodes) {
            answer.push_back(level.second);
        }

        return answer;
    }
};

Receive Honest News Today

Join over 4 million Americans who start their day with 1440 – your daily digest for unbiased, fact-centric news. From politics to sports, we cover it all by analyzing over 100 sources. Our concise, 5-minute read lands in your inbox each morning at no cost. Experience news without the noise; let 1440 help you make up your own mind. Sign up now and invite your friends and family to be part of the informed.

Recommendations

Level up your inbox by subscribing to what I am subscribed to!

Hacks of the week

  1. Extract ANYYYY linkedin profile using Rocket Reach

  2. Join VC newsletters, they send jobs!!

Self-gaslight of the week

I truly believe you can gaslight yourself to success.

  1. Look at all of the things I’ve survived, there is no way I’m not the main character

  2. How to avoid the worse feeling in your life

Cheers to you hacking your week,

Denisse

Reply

or to participate.