Left view of binary tree

the code is working fine ,but why i need to write maxlvl in the leftview function again i.e maxlvl =-1 ,as i have already declared the global maxlvl, without it the code is not workiing…??

code :

int maxlvl=-1;
void solve(Node* root,int d)
{
if(root==NULL)
return;
if(d>maxlvl)
{
cout<data<<" ";
maxlvl=d;
}
solve(root->right,d+1);
solve(root->left,d+1);
}

void rightView(Node *root)
{
// Your Code here
maxlvl=-1;
solve(root,0);

}

@firozbhaikardar21 hey if you declare maxlevel with -1 globally ,then you don’t have to mention it again.

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.