Left View of Binary Tree | Practice | GeeksforGeeks

Given a Binary Tree, print Left view of it. Left view of a Binary Tree is set of nodes visible when tree is visited from Left side. The task is to complete the function leftView(), which accepts root of the tree as argument.
Left view of following tree is 1 2 4 8.
          1
       /     \
     2        3
   /     \    /    \
  4     5   6    7
   \
     8   
Example 1:
Input:
   1
 /  \
3    2
Output: 1 3


Example 2:
Input:

Output: 10 20 40

Your Task:
You just have to complete the function leftView() that prints the left view. The newline is automatically appended by the driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
0 <= Number of nodes <= 100
1 <= Data of a node <= 1000
0
binayshaw7777in 2 hours
Simple Java Code:
class Tree
{
    //Function to return list containing elements of left view of binary tree.
    ArrayList<Integer> leftView(Node root) {
      ArrayList<Integer> list = new ArrayList<>();
      LV(root, list, 0);
      return list;
    }
    
    public void LV(Node root, ArrayList<Integer> list, int level) {
        if (root == null) return;
        if (level == list.size()) list.add(root.data);
        LV(root.left, list, level+1);
        LV(root.right, list, level+1);
    }
}
0
tarunkanade1 day ago
0.83/1.91 Java Theta(n), Theta(w)
ArrayList<Integer> leftView(Node root)
    {
      // Your code here
      ArrayList<Integer> list = new ArrayList<>();
      
      if(root == null){
          return list;
      }
      
      ArrayDeque<Node> q = new ArrayDeque<>();
      int count;
      Node cur;
      
      q.offer(root);
      
      while(!q.isEmpty()){
          count = q.size();
          
          for(int i=0; i<count; i++){
              cur = q.poll();
              if(i == 0){
                  list.add(cur.data);
              }
              
              if(cur.left != null) q.offer(cur.left);
              if(cur.right != null) q.offer(cur.right);
          }
      }
      
      return list;
    }
0
shulabhgill2 days ago
class Tree{   //Function to return list containing elements of left view of binary tree.   ArrayList<Integer> leftView(Node root)   {     // Your code here             ArrayList<Integer> res =new ArrayList<>();           if(root==null) return res;     Queue<Node> qu=new LinkedList<>();     qu.add(root);          while(!qu.isEmpty()){                  int k= qu.size();                  for(int i=0;i<k;i++){             Node temp=qu.poll();             if(i==0) res.add(temp.data);                          if(temp.left!=null)             qu.add(temp.left);                             if(temp.right!=null)             qu.add(temp.right);         }              }         return res;              }}
0
shubham211019973 days ago
ArrayList<Integer> arr=new ArrayList<Integer>();
     if(root==null)return arr;
     Queue<Node>q=new LinkedList<>();
     q.add(root);
    while(q.isEmpty()==false){
        int size=q.size();
        for(int i=0;i<size;i++){
            Node curr=q.poll();
            if(i==0){
                arr.add(curr.data);
            }
            if(curr.left!=null)q.add(curr.left);
            if(curr.right!=null)q.add(curr.right);
        }
    }
    return arr;
0
iamtkarthikreddy
This comment was deleted.
0
osamu1 week ago
vector<int> leftView(Node *root){  // Your code here  vector<int> ans;       if(!root)           return ans;       queue<pair<Node*,int>> q;  // Store Node and level       q.push({root,0});       map<int,int> mp; //store level and value at that level       while(!q.empty())       {           auto it= q.front();           q.pop();           Node* curr=it.first;           int lev=it.second;           if(mp.count(lev)==0)  // in this condition it will check if on that level there exist an element than it will not work else it will insert a value at that key making its count as 1 so after next time it will not work           {               mp[lev]=curr->data;           }           if(curr->left)           {               int temp=lev+1;               q.push({curr->left,temp});           }           if(curr->right)           {               int temp=lev+1;               q.push({curr->right,temp});           }       }       for(auto it: mp)       {           ans.push_back(it.second);       }       return ans;}
 
//Basically what are we doing is we are making a queue of pair<Node*,int> where here int will be the level as its left view so if any level is counter first during bfs we will make it put in map in which map will have key as level and value as data
+3
bipulharsh1232 weeks ago
void getLeftView(Node *ptr, vector<int>&result, int level=1){   if(!ptr)       return;   if(level > result.scd ize())       result.push_back(ptr->data);   getLeftView(ptr->left, result, level+1);   getLeftView(ptr->right, result, level+1);}
//Function to return a list containing elements of left view of the binary tree.vector<int> leftView(Node *root){  // Your code here  vector<int> result;  int level=1;  getLeftView(root, result, level);  return result;}
+2
jainmuskan5652 weeks ago
vector<int>v;void fxn(Node* node, int level){   if(node== NULL){       return;   }   if(v.size()<= level){       v.push_back(node->data);   }   fxn(node->left, level+1);   fxn(node->right, level+1);}vector<int> leftView(Node *root){  // left side view  // first node of every level   // traversing left root right <--  v.clear();  fxn(root,0);  return v;  }
0
mkbastronaut4412 weeks ago
vector<int> leftView(Node *root){  // Your code here    //Your code here   if(root==NULL)     return {};  queue<Node*>q;  vector<int> v;  q.push(root);  while(!q.empty()){      int n=q.size();      for(int i=0;i<n;i++){          Node*t=q.front();          q.pop();          if(i==0) v.push_back(t->data);          if(t->left) q.push(t->left);          if(t->right)q.push(t->right);      }  }  return v;
0
jatin2k19ec0822 weeks ago
int height(Node* root){   if(root==NULL)   return 0;   int lh=height(root->left);   int rh=height(root->right);   if(lh>rh)   return lh+1;   else   return rh+1;}int left(Node* root,int level){   if(root==NULL)   return 0;      if(level==1)   {      if(root==NULL)      return 0;      else      return root->data;   }   int k=left(root->left,level-1);   if(k==0)   return left(root->right,level-1);   else   return k;}vector<int> leftView(Node *root){  // Your code here  vector<int>arr;  int h=height(root);  for(int i=1;i<=h;i++)  {      int j=left(root,i);      arr.push_back(j);  }  return arr;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.