Topic
Topic
From our observation, we can find that all the child nodes have exchanged positions, so we only need to recurse from the root node, and then exchange the left and right sons of the root node.
Code
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void mirror(TreeNode* root) {
if(!root) return;
mirror(root->left);
mirror(root->right);
swap(root->left,root->right);
}
};