def invertTree(self, root: TreeNode) -> TreeNode:
if root != None:
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
else:
return None
return root
cpp:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (!root)
return NULL;
TreeNode* tmp = root->right;
root->right = invertTree(root->left);
root->left = invertTree(tmp);
return root;
}
};