问题
关键:利用递归和树的知识
代码
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
void mirror(struct TreeNode* root) {
void Swap(struct TreeNode* root,struct TreeNode* root1,struct TreeNode* root2);
if(!root) return;
Swap(root,root->left,root->right); //带上father
mirror(root->left);
mirror(root->right);
return;
}
void Swap(struct TreeNode* root,struct TreeNode* root1,struct TreeNode* root2){
if(!root1 || !root2){
if(root1){
root->right = root1;
root->left = NULL;
// Swap(root1,root1->left,root1->right);
return;
}
else if(root2){
root->left = root2;
root->right = NULL;
// Swap(root2,root2->left,root2->right);
return;
}
else return;
}
//两个结点都存在
else{
root->left = root2;
root->right = root1;
return;
}
}