Interview Questions

182. Write a code to print level order traversal of N-ary tree .......

Microsoft Interview Questions and Answers


(Continued from previous question...)

182. Write a code to print level order traversal of N-ary tree .......

Question:
Write a code to print level order traversal of N-ary tree
data structure can be assumed as
class Node{
vector<Node> children;
int val;
}



maybe an answer:


#define MAX_CHILD_NODES 10

typedef struct node{
int val;
struct node *child[MAX_CHILD_NODES];
}tNode;

void LeveTraversal(tNode *root)
{
if( root == NULL )
{
return ;
}
else
{
tNode *p = NULL ;
QueueADD(root);

while( !QueueEmpty() )
{
p = QueueDelete();
printf(" %d ", p->val );
for( int i = 0 ; i<MAX_CHILD_NODES ; i++ )
{
if( p->child[i] == NULL )
{
break;
}
else {
QueueAdd( p->child[i] );
}
}
}
}
}

(Continued on next question...)

Other Interview Questions