翻转一棵二叉树。
思路:dfs
# 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:
if not root:
return root
def jx(root):
root.left,root.right = root.right,root.left
if root.left:
jx(root.left)
if root.right:
jx(root.right)
jx(root)
return root