27.二叉树的镜像
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
输入:
4
/ \
2 7
/ \ / \
1 3 6 9
镜像输出:
4
/ \
7 2
/ \ / \
9 6 3 1
思路
交换左右子树 递归
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(!root) return root;
TreeNode* tmp = root->right;
root->right = mirrorTree(root->left);
root->left = mirrorTree(tmp);
return root;
}
};