why isn’t my code giving expected output?
#include
#include
using namespace std;
class node{
public:
int data;
node *left,*right;
node(int data)
{
this->data = data;
left = NULL;
right = NULL;
}
};
node* buildTree(){
int d;
cin >> d;
if(d==-1)return NULL;
node*root = new node(d);
root->left = buildTree();
root->right = buildTree();
return root;
}
void leftView(node *root,int level,int& max_level){
if(root == NULL)
return;
if (max_level<level){
cout<<root->data<<" ";
max_level = level;
}
leftView(root->left,level+1,max_level);
leftView(root->right,level+1,max_level);
}
int main(){
node* root = buildTree();
int max_level = -1;
leftView(root,0,max_level);
return 0;
}