剑指 Offer 27. 二叉树的镜像(python/c++)
作者:互联网
/**
* 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*left = mirrorTree(root->left);
TreeNode*right = mirrorTree(root->right);
root->left = right;
root->right = left;
return root;
}
};
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
if not root:return
root.left,root.right = self.mirrorTree(root.right),self.mirrorTree(root.left)
return root
标签:27,TreeNode,Offer,self,right,二叉树,mirrorTree,root,left 来源: https://blog.csdn.net/qq_36421001/article/details/120404958