Tree bottom view- Please explain the question

how to take the level order input of the tree?

hey @ayushjain.aj300, here is the code for same
node* bulidPreTree()
{
int d;
cin>>d;
if(d==-1)
{
return NULL;
}
node* root=new node(d);
root->left=bulidPreTree();
root->right=bulidPreTree();
return root;
}

int main()
{
node* root=bulidPreTree();
printLevelOrder(root);
}

Sir You are taking input in Preordered manner. But I asked level order input,not level order traversal

hey @ayushjain.aj300, sorry for that. here is the logic
Node* createTreeFromLevelOrder()
{
int d;
cin>>d;
Node* root=new Node(d);
queue<Node*> q;
q.push(root);
while(!q.empty())
{
int d1,d2;
cin>>d1>>d2;
Node* f=q.front();
if(d1!=-1)
{
f->left=new Node(d1);
q.push(f->left);
}
if(d2!=-1)
{
f->right=new Node(d2);
q.push(f->right);
}
q.pop();
}
return root;
}

Thanks for the help Sir. :slight_smile:

1 Like