Interview Questions

233. Print all edge nodes of a complete binary tree anti-clockwise ... .....

Microsoft Interview Questions and Answers


(Continued from previous question...)

233. Print all edge nodes of a complete binary tree anti-clockwise ... .....

Question:
Print all edge nodes of a complete binary tree anti-clockwise. That is all the left most nodes starting at root, then the leaves left to right and finally all the rightmost nodes. In other words, print the boundary of the tree.
Variant: Print the same for a tree that is not complete.


maybe an answer:


void PrintLeafNode(Tree *root)
{
if(root != NULL)
{
if(root->left == NULL && root->right == NULL)
printf("\n %d", root->data);
PrintLeafNode(root->left);
PrintLeafNode(root->right);
}
}

void PrintLEdges(Tree *root)
{
if(root != NULL)
{
printf("\n %d", root->data);
PrintLEdges(root->left);
PrintLeafNode(root->right);
}
}

void PrintREdges(Tree *root)
{
if(root != NULL)
{
PrintLeafNode(root->left);
PrintREdges(root->right);
printf("\n %d", root->data);
}
}

int _tmain(int argc, _TCHAR* argv[])
{
int arr[] = {50,40,70,30,45,60,90,42,47,55,65,80};
Tree *root = NULL;

for(int i=0; i<11; i++)
root = CreateTree(root, arr[i]);

printf("\n %d",root->data);

PrintLEdges(root->left);
PrintREdges(root->right);

return 0;
}

(Continued on next question...)

Other Interview Questions