/** * 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