题目:
操作给定的二叉树,将其变换为源二叉树的镜像
通用基础知识点:二叉树的遍历,递归算法
核心点:二叉树的递归遍历,交换
#include<iostream>
using namespace std;
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if (pRoot == nullptr)
return;
if (pRoot->left == nullptr&&pRoot->right == nullptr)
return;
TreeNode *pTemp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = pTemp;
if (pRoot->left)
Mirror(pRoot->left);
if (pRoot->right)
Mirror(pRoot->right);
}
};