1.递归法

    1. void Mirror(TreeNode *pRoot) {
    2. if(!pRoot || (pRoot->left == nullptr && pRoot->right == nullptr))return;//或者只是if(!pRoot)
    3. TreeNode* temp = pRoot->left;
    4. pRoot->left = pRoot->right;
    5. pRoot->right = temp;
    6. if(pRoot->left)Mirror(pRoot->left);
    7. if(pRoot->right)Mirror(pRoot->right);
    8. }

    2.非递归

    void Mirror(TreeNode *pRoot) {
            if(!pRoot)return;
            queue<TreeNode*> q;
            q.push(pRoot);
            TreeNode *tr, *temp;
            while(!q.empty()){
                tr = q.front();
                if(tr->left || tr->right){
                    temp = tr->left;
                    tr->left = tr->right;
                    tr->right = temp;
                    if(tr->left)q.push(tr->left);
                    if(tr->right)q.push(tr->right);
                }
                q.pop();
            }
        }