翻转二叉树
# 翻转一棵二叉树。
#
# 示例:
#
# 输入:
#
# 4
# / \
# 2 7
# / \ / \
# 1 3 6 9
#
# 输出:
#
# 4
# / \
# 7 2
# / \ / \
# 9 6 3 1
方法:递归,子树处理
# leetcode submit region begin(Prohibit modification and deletion) # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if not root: return root.left, root.right = self.invertTree(root.right), self.invertTree(root.left) return root # leetcode submit region end(Prohibit modification and deletion)
时刻记着自己要成为什么样的人!