Title description
Operate the given binary tree and transform it into a mirror image of the source binary tree.
Enter description:
Mirror definition of binary tree: source binary tree
8
/ \
6 10
/ \ / \
5 7 9 11
Mirror binary tree
8
/ \
10 6
/ \ / \
11 9 7 5
Recursive thinking
Answer code:
/*
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 *root) {
if(!root){
return;
}
TreeNode * temp = root->left;
root->left = root->right;
root->right = temp;
Mirror(root->left);
Mirror(root->right);
}
};
If you have different opinions, please leave a message to discuss ~~