Amazon Interview Question
Developer Program Engineersvoid enqueChildren(queue<pnode> &q, pnode node)
{
if(!node)
return;
for(int i = 0; i < node->children; i++)
{
q.push(*(node->arrChildren)++);
}
}
bool IsInNaryTree(pnode root, pnode node)
{
if(!root || !node)
return false;
queue<pnode> qnode;
qnode.push(root);
while(qnode.front())
{
if(qnode.front() == node)
return true;
enqueChildren(qnode, qnode.front());
qnode.pop();
}
return false;
}
Insert head into queue.
- putta.sreenivas May 09, 2011now run a loop until the queue is empty. inside the loop
1.dequeue the front of the queue. if it is equal to our data, print it and return.
else
2.Use a function called getchildnodes(node *head) to get all the childs of a particular node.
3. enquue all these child nodes into the queue.
4. repeat the process until queue becomes empty.