Skip to content

Latest commit

 

History

History
34 lines (27 loc) · 869 Bytes

File metadata and controls

34 lines (27 loc) · 869 Bytes

Left view of the binary tree

  • Left view is nothing but first element if we traverse level wise.
  • last element would fall in right view category.
Code
vector<int> leftView(Node *root){
   if (root == nullptr) return {};
   vector<int> ans; 
   queue<Node*> qu; 
   qu.push(root);
   
   while (!qu.empty()) {
       int Size = qu.size(); 
       vector<int> temp; 
       
       for (int i = 0; i < Size; i++) {
           auto root = qu.front(); 
           qu.pop(); 
           
           temp.push_back(root -> data); 
           
           if (root -> left) qu.push(root -> left); 
           if (root -> right) qu.push(root -> right); 
       }
       if (!temp.empty()) ans.push_back(temp.front());
   }
   return ans; 
}