Given a binary tree where initially sibling pointers....
Microsoft Interview Questions and Answers
(Continued from previous question...)
67. Given a binary tree where initially sibling pointers....
Question:
Given a binary tree where initially sibling pointers(left to right) are null for all nodes, connect all nodes at each level.
When I gave a solution with BFS, interview wanted a iterative solution as he feels BFS is easy solution.
maybe an answer:
void ConnectSiblings(struct node *r)
{
struct node *temp;
if(!r)
return;
enquee1(r);
while(front1!=NULL || front2!=NULL)
{
while(front!=NULL)
{
temp=dequee1();
if(temp->left)
enquee2(temp->left);
if(temp->right)
enquee2(temp->right);
temp->sibling=dequee1();
}
while(front2!=NULL)
{
temp=dequee2();
if(temp->left)
enquee1(temp->left);
if(temp->right);
enquee(temp->right);
temp->sibling=dequee2();
}
}
return;
}
(Continued on next question...)
Other Interview Questions
|