What is the problem with my code?

its not passing all test cases.
#include
#include
using namespace std;
class node{
public:
int data;
node* lt;
node* rt;
node(int d){
data=d;
lt=NULL;
rt=NULL;
}
};
node* buildt(){
int d,cnt=0;
queue<node*> a;
cin>>d;
node* rot=NULL;
node* cur=NULL;
if(d!=-1){
rot=new node(d);
cur=rot;
while(1){
cin>>d;
if(d!=-1){
if(cnt==0){
cur->lt=new node(d);
a.push(cur->lt);
cnt++;}
else{
cur->rt=new node(d);
a.push(cur->rt);
cnt=0;
cur=a.front();
a.pop();
}
}
else{
if(cnt==0){cnt++;}
else{
cnt=0;
cur=a.front();
a.pop();
}
}
if(a.empty()&&cnt==0){return rot;}
}
}
return rot;
}
vector leftView(node rot)
{
queue<node
> q;
vector vv;
if(rot) q.push(rot);
while(!q.empty()){
int n= q.size();
for(int i=0;i<n;i++){
node* tem = q.front();
if(i==0){
vv.push_back(tem->data);}
if(tem->lt){
q.push(tem->lt);}
if(tem->rt){
q.push(tem->rt);}
q.pop();
}
}
return vv;
}
int main(){
node* bt= buildt();
if(!leftView(bt).empty()){
for(auto x:leftView(bt)){
cout<<x<<" ";
}}
return 0;
}

hi @dsinght4
The problem can also be solved using simple recursive traversal. We can keep track of level of a node by passing a parameter to all recursive calls. The idea is to keep track of maximum level also. Whenever we see a node whose level is more than maximum level so far, we print the node because this is the first node in its level (Note that we traverse the left subtree before right subtree).

refer this code -->

void leftViewHelper(node *root, int level, int &max_level) {
    if(root == NULL) {
        return;
    }
    if(max_level<level) {
        cout<<root->data<<" ";
        max_level = level;
    }
    leftViewHelper(root->left, level+1, max_level);
    leftViewHelper(root->right, level+1, max_level);
}

void leftView(node *root) {
    int max_level = 0;
    leftViewHelper(root, 1, max_level);
}

Iterative Approach

  • Do a level order traversal.
  • Push NULL into the queue to mark the end of each level.
  • Print the data of root of data and start with level order traversal.
  • As you encounter a NULL, print the next element.
  • Otherwise skip all the other elements

C++ Code for Iterative Approach

void printLeftSide(node *root)
{
    queue<node *> q;
    q.push(root);
    q.push(NULL);

    bool flag = true;

    while (!q.empty())
    {
        node *f = q.front();
        if (f == NULL)
        {
            q.pop();
            if (!q.empty())
            {
                q.push(NULL);
            }
            flag = true;
        }
        else
        {
            if (flag)
            {
                cout << f->data << " ";
                flag = false;
            }
            q.pop();

            if (f->left)
            {
                q.push(f->left);
            }
            if (f->right)
            {
                q.push(f->right);
            }
        }
    }
    return;
}

I hope it clears all ur doubts…

hi @dsinght4
if u still face any issues u can try my code…


it passed all test cases…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.