Trouver le nœud parent de l'arbre (langage C)

1. Définition de la notation de frère enfant C

typedef struct Node {
    int data;
    struct Node * leftChild;
    struct Node * rightSibling;
)TreeNode;

Deux, le code

TreeNode * findParent (TreeNode * root, TreeNode * q){
    // root为根节点, 寻找节点q的双亲

    TreeNode * p = roo;
    if (root == NULL || root->leftChild == NULL)
        return NULL;
    else if (root->leftChild->data == q->data)
        return root;
    else {
        p = root->leftChild->rightSibling;
        while (p) {
               if (p->data == q->data){
                    return root;
                else
                    p = p->rightSibling;
        }
    }
    if ( p = findParent(root->leftChild)
        return p;
    else
        return findParent(root->leftChild->rightSibling);
}

Je suppose que tu aimes

Origine blog.csdn.net/weixin_43537097/article/details/128501091
conseillé
Classement