深度搜索(递归也一样)
class Solution {
public:
void towmirrortree(TreeNode* root){
if(root==NULL)return;
if(root->left==NULL&&root->right==NULL)return;
auto temp=root->left;
root->left=root->right;
root->right=temp;
towmirrortree(root->left);
towmirrortree(root->right);
}
TreeNode* mirrorTree(TreeNode* root) {
towmirrortree(root);
return root;
}
};
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/315bd5f91c7b50548711bee85b7568e0.png)
递归
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(root==NULL)return NULL;
if(root->left==NULL&&root->right==NULL)return root;
auto temp=root->left;
root->left=mirrorTree(root->right);
root->right=mirrorTree(temp);
return root;
}
};
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8bf7399775ffcbd3527c5dd3cee6d738.png)