Mirror image of a binary tree

前端 未结 13 2537
时光取名叫无心
时光取名叫无心 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:03

    struct node *MirrorOfBinaryTree( struct node *root)
    { struct node *temp;
    if(root)
    {
    MirrorOfBinaryTree(root->left);
    MirrorOfBinaryTree(root->right);
    /*swap the pointers in this node*/
    temp=root->right;
    root->right=root->left;;
    root->left=temp;
    }
    return root;
    }
    

    Time complexity: O(n) Space complexity: O(n)

提交回复
热议问题