226 翻转二叉树
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
self.invertTree(root.left)
self.invertTree(root.right)
root.left,root.right=root.right,root.left
return root
101 对称二叉树
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
if not root:
return False
return self.compare(root.left,root.right)
def compare(self,l,r):
if not l and r:
return False
elif not r and l:
return False
elif not l and not r:
return True
elif l.val!=r.val:
return False
out=self.compare(l.left,r.right)
ins=self.compare(l.right,r.left)
return out and ins