Traverse the tree in descending order and keep a count of number of nodes visited. When this count is equal to 'k', print the element.
void printk(struct node* root, int k)
{
if(root == NULL)
return;
static int index = 0;
printk(root->right, k);
if(++index == k)
{
printf("%d\n", root->data);
return;
}
printk(root->left, k);
}
No comments:
Post a Comment