Getting runtime error but cant understand why ......plzzzzzzz look into this


#1

int findlevel(TreeNode* A, int B,int l)
{
if(A==NULL)
return 0;

 if(A->val==B)
 return l;
 
 int k;
 
 k=findlevel(A->left,B,l+1);
 
 if(k!=0)
 return k;
 
 k=findlevel(A->right,B,l+1);
 
 return k;

}
void printlevel(TreeNode* A,int k,vector &v,int B)
{
if(A==NULL)
return ;
if(k==1)
v.push_back(A->val);

 if(A->left->val==B || A->right->val==B)
 {
     return ;
 }
 else
 printlevel(A->left,k-1,v,B);

 
if(A->left->val==B || A->left->val==B)
{
    return;
}
else
 printlevel(A->right,k-1,v,B);

}
vector Solution::solve(TreeNode* A, int B)
{ int k;

vector v;
k=findlevel(A,B,1);

printlevel(A,k,v,B);

return v;

}
.


#2

See in printlevel function if A is leaf node and k==1 then what happens is you printed the value and then again checking for A->left->val while A->left is alredy null .Here you will encounter runtime error .


#3

use level order traversal