Skip to main content

Command Palette

Search for a command to run...

Binary Trees - Left View Of Binary Tree

Published
1 min readView as Markdown
Binary Trees - Left View Of Binary Tree
N
This is Nirbhay Singh currently working as a Software Engineer 1 at Hashedin by Deloitte. I have majorly worked in Springboot and Django but I also have little bit of hands on in React and slowly transitioning to Full stack developer by leveraging AI. My goal right now is to learn and gain more experience in this industry and work on amazing projects

Left View Of Binary Tree

https://www.codingninjas.com/studio/problems/left-view-of-binary-tree_625707?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf

Hint video:

Code:

vector<int> printLeftView(BinaryTreeNode<int>* root) {
    // Write your code here.

    vector<int> view;
    queue<pair<BinaryTreeNode<int>*,int>> q;

    if(root!=NULL){
        q.push({root,0});
    }

    while(!q.empty()){
        BinaryTreeNode<int>* node = q.front().first;
        int height = q.front().second;
        q.pop();

        if(height+1>view.size()){
            view.push_back(node->data);
        }

        if(node->left!=NULL){
            q.push({node->left,height+1});
        }

        if(node->right!=NULL){
            q.push({node->right,height+1});
        }
    }

    return view;
}

DSA prep

Part 4 of 22

This series is specifically to document and share my learning of Data Structure and Algorithms and building the programmers intuition to solve a problem through coding and getting my first job as SDE.

Up next

Graph - Find the Number of Provinces

Find the Number of Provinces Problem Link: https://www.codingninjas.com/studio/problems/find-the-number-of-states_1377943?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf https://leetcode.com/problems/number-of-provinces/description/...

More from this blog

Daily Code by Nirbhay

74 posts

Hey, this is Nirbhay. I started this blog to document my journey of learning to code and get my first $100k offer. I'll be sharing the things related to DSA, backend development, devops and many more.