Web Toolbar by Wibiya

Pages

Friday, January 20, 2012

Print nodes at level k in a Binary Tree.

Solution #1: Here is the recursive code,

void printLevel(struct node* root, int k)
{
    if(root == NULL)
        return;

    if(k == 1)
    {
        printf("%d\n", root->data);
        return;
    }

    printLevel(root->left, k-1);
    printLevel(root->right, k-1);
}

No comments: