Question: Print all edge nodes of a complete binary tree anti-clockwise. In other words, print the boundary of the tree.
Solution:
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 PrintLeftEdges(Tree *root)
{
if(root != NULL)
{
printf("\n %d", root->data);
PrintLeftEdges(root->left);
PrintLeafNode(root->right);
}
}
void PrintRightEdges(Tree *root)
{
if(root != NULL)
{
PrintLeafNode(root->left);
PrintRightEdges(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);
PrintLeftEdges(root->left);
PrintRightEdges(root->right);
return 0;
}