DAY4
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
例如输入:
4
/
2 7
/ \ /
1 3 6 9
镜像输出:
4
/
7 2
/ \ /
9 6 3 1
思路:递归交换左右子树
/**
- 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:
TreeNode* mirrorTree(TreeNode* root) {if(root==NULL){return root;}
TreeNode* right_tree=mirrorTree( root->left); TreeNode* left_tree=mirrorTree( root->right); root->left=left_tree; root->right=right_tree; return root; }
};