2020-03-21
1.题目描述
二叉树的镜像
2.题解
又是一个树的问题,使用递归比较好求解
3.代码
#include <iostream>
#include <algorithm>
using namespace std;
// 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) return NULL;
TreeNode* tmp = (TreeNode*)malloc(sizeof(TreeNode));
if (tmp){
tmp->val=root->val;
tmp->left=mirrorTree(root->right);
tmp->right=mirrorTree(root->left);
}
return tmp;
}
};
int main(){
Solution s;
return 0;
}