class Solution {
public:
void towmirrortree(TreeNode* root){
if(root==NULL)return;
if(root->left==NULL&&root->right==NULL)return;
auto temp=root->left;
root->left=root->right;
root->right=temp;
towmirrortree(root->left);
towmirrortree(root->right);
}
TreeNode* mirrorTree(TreeNode* root) {
towmirrortree(root);
return root;
}
};
递归
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(root==NULL)return NULL;
if(root->left==NULL&&root->right==NULL)return root;
auto temp=root->left;
root->left=mirrorTree(root->right);
root->right=mirrorTree(temp);
return root;
}
};
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)