题目大意
解题思路
- 首先明确什么是二叉树的镜像
- 直接通过递归swap即可,需要注意的是递归结束的标志是处理的节点为空节点。
代码
class Solution {
public:
void mirrorMyProcess(TreeNode* root){
if(!root){
return;
}
swap(root->left,root->right);
if(root->left){
mirrorMyProcess(root->left);
}
if(root->right){
mirrorMyProcess(root->right);
}
}
TreeNode* mirrorTree(TreeNode* root) {
TreeNode* tem = root;
mirrorMyProcess(root);
return tem;
}
};