Citrix Online Interview Question
Software Engineer / DevelopersDo a BFS, maintain a map, before pushing the child of every node to queue, push the node and its parent to map. Once the node is found, use the map to find the parent and push the parent in stack till NULL parent is found, at that time pop out the elements of stack and print them
the question is to print route sequentially, so the correct one would be;
print_route(node * N, int key)
{
if(N==null)
exit(0);
if(key< N->value)
{
insert(L,n->value);
N=N->lchild;
print_route(N,key);
}
else if(key>N-> value)
{
insert(L,n->value);
N=N->rchild;
print_route(N,key);
}
else if(key==N->value)
{ insert(L,n->value);
}
while (L!=null)
{
printf("%d",L->info)
L=L->next;
}
}
insert(list* L, int k)
{
node=(list*)malloc(sizeof(struct list));
if(L==null)
{ L=node;
}
else if(node->info<L->info)
{
node->next=L;
L=node;
}
else
{
list* first=L;
list*nxtptr;
while (node->info > first->info)
{
nxtptr=first;
first=first->next;
}
nxtptr->next=node;
node->next=first;
}
return L;
}
- chenming831 June 16, 2010