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);
}