【问题】操作给定的二叉树,将其变换为源二叉树的镜像。

二叉树的镜像定义:源二叉树

【思路】这个使用递归的思路就很简单,一般二叉树用递归的方法很多,比如二叉树的遍历也可以使用递归的方法。我们首先交换左右子树的位置,因此使用tmp用于转换存储,然后递归转换就可以了!并且递归最重要的就是终止条件,这里的终止条件也很简单,递归到叶节点的儿子,则return,结束递归!

 1/*
 2struct TreeNode {
 3    int val;
 4    struct TreeNode *left;
 5    struct TreeNode *right;
 6    TreeNode(int x) :
 7            val(x), left(NULL), right(NULL) {
 8    }
 9};*/
10class Solution {
11public:
12    void Mirror(TreeNode *pRoot) {
13        if(pRoot == nullptr)
14            return;
15        TreeNode* tmp = pRoot->left;
16        pRoot->left = pRoot->right;
17        pRoot->right = tmp;
18        Mirror(pRoot->left);
19        Mirror(pRoot->right);
20    }
21};