URL:https://www.nowcoder.com/practice/a9d0ecbacef9410ca97463e4a5c83be7?tpId=13&tqId=11171&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking&tab=answerKey

    1. /**
    2. * struct TreeNode {
    3. * int val;
    4. * struct TreeNode *left;
    5. * struct TreeNode *right;
    6. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    7. * };
    8. */
    9. class Solution {
    10. public:
    11. /**
    12. * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    13. *
    14. *
    15. * @param pRoot TreeNode类
    16. * @return TreeNode类
    17. */
    18. TreeNode *Mirror(TreeNode *pRoot) {
    19. // write code here
    20. if (pRoot == nullptr) {
    21. return nullptr;
    22. }
    23. TreeNode *tmp = pRoot->left;
    24. pRoot->left = pRoot->right;
    25. pRoot->right = tmp;
    26. Mirror(pRoot->right);
    27. Mirror(pRoot->left);
    28. return pRoot;
    29. }
    30. };