题目链接
方法1 递归
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def invertTree(self, root: TreeNode) -> TreeNode:
def digui(root):
if not root:
return
if root.left and root.right:
root.left,root.right=root.right,root.left
elif root.left :
root.right=root.left
root.left=None
elif root.right:
root.left=root.right
root.right=None
else:
return
digui(root.left)
digui(root.right)
return
digui(root)
return root