题目描述:
第一次提交:
class Solution(object):def invertTree(self, root):""":type root: TreeNode:rtype: TreeNode"""if not root:return Nonetemp = root.leftroot.left = root.rightroot.right = temp# root.left,root.right = root.right,root.leftself.invertTree(root.left)self.invertTree(root.right)return root
可缩减代码:
class Solution(object):def invertTree(self, root):""":type root: TreeNode:rtype: TreeNode"""if not root :return rootelse:root.left,root.right=self.invertTree(root.right),self.invertTree(root.left)return root