Getting some error for only one case not able to find it

check my code given

left view problem …check mistake …1 case is not passed

for the i/p
1 2 3 4 -1 5 -1 -1 -1 6 -1 -1 -1
tree should be like

ur code gives bfs result


which should be
1
2 3
4 5
6
please check if there is some problem with the way u form the tree

1 Like

@chhavibansal not able to get the problem…pl. check

now found the problem still 1 test case is not passed
@chhavibansal
code-:

bro @lakshaygarg128
this code does not even produce correct answer sample teest case
please verify if u have given the correct code or not

#include<bits/stdc++.h>

using namespace std;

class node
{
public:
int data;
node *left,*right;

node(int d)
{
    data=d;
    left=right=NULL;
}

};

// Breadth First Search (BFS)
void BFS(node root)
{
queue<node
> q;

q.push(root);
q.push(NULL);	//	To print each level on new line
	
while(!q.empty())
{
	node *f=q.front();
	if(f==NULL)
	{
		cout<<"\n";
		q.pop();
		
		if(!q.empty())
		q.push(NULL);
	}
	
	else
	{
		cout<<f->data<<" ";
		q.pop();
	
	if(f->left)
		{
			q.push(f->left);
		}
	
	if(f->right)
		{
			q.push(f->right);
		}
	
	}
	
}

}

node buildtree()
{
int count=0;
int d;
queue<node
> q;

cin>>d;
node *root=new node(d);
node *cur=root;
root=cur;
node * temp=cur;

while(1)
{  node *temp=cur;
    cin>>d;
    if(d!=-1)
    {
        if(count==0)
        {
            cur->left=new node(d);
            count++;
            q.push(cur->left);
    
        }

        else if(count==1)
        {
            cur->right=new node(d);
            q.push(cur->right);
            
            count=0;
            
            if(q.front())
            cur=q.front();
            
            q.pop();
        }
    } 

    else if(d==-1)
    {
        if(count==0)
        {
            
        count++;
        }
        else
        {
            count=0;
            if(q.front() )
            cur=q.front();
            q.pop();
        }
    }  
     
    if((q.empty()) && (temp==cur)   )
    return root;
   

    
}

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

}

int main()
{
node *root=buildtree();

// BFS(root);
int max=-1;
leftview(root,0,max);

return 0;

}

check the which problem u r checking… mine sample case is correct

share code in cb ide

have u got the correct code…and checked on correct problem???

doing man
i am debugging it seems the tree is not formed correctly

for this i/p
ur tree is wrong
1 2 3 -1 4 -1 5 -1 -1 -1 6 -1 7 -1 8 -1 -1

i`ll provide the correct code
wait for a while

ok…sure…waiting for the reply… :slightly_smiling_face: :slightly_smiling_face:

updated the buildtree() function now
it should submit fine

i have understood the problem but i m not able to debug…

please check out the code
buildTree code had the problem i have replaced it

now it is working…thank you :innocent: :innocent:

1 Like