Algorithm Find the level of the node whose node value is x in a binary tree

Hits: 0

Find the level of the node whose node value is x, considering the unique case of the node

int find_node_level(BTNode *bt, ElemType x, int h)
{
    if (bt == NULL)
        return 0;
    else if (bt->data == x)
        return h;
    else
    {
        int l = find_node_level(bt->lchild, x, h+1);
        if (l != 0)
            return l;
        else
            return find_node_level(bt->rchild, x, h+1);
    }
}

Ideas: Node search –>> traverse the [binary tree] ; to determine the current level, use the function parameters and return values ​​to transmit information.

You may also like...

Leave a Reply

Your email address will not be published.