Mirror image of a binary tree

前端 未结 13 2495
时光取名叫无心
时光取名叫无心 2020-12-01 09:00

Suppose there is a tree:

             1
            / \\
           2   3
              / \\
             4   5

Then the mirror image will

13条回答
  •  北海茫月
    2020-12-01 09:04

    Here is my function. Do suggest if any better solution:

    void mirrorimage(struct node *p)
    {
        struct node *q;
        if(p!=NULL)
        {
            q=swaptrs(&p);
            p=q;
            mirrorimage(p->left);
            mirrorimage(p->right);
        }
    }
    
    struct node* swaptrs(struct node **p)
    {
        struct node *temp;
        temp=(*p)->left;
        (*p)->left=(*p)->right;
        (*p)->right=temp;
        return (*p);
    }
    

提交回复
热议问题