Python3实现翻转二叉树问题
翻转一棵二叉树。
# 二叉树的结构如下
class TreeNode:def __init__(self, x):self.val = xself.left = Noneself.right = None# 解决方案
class Solution:# 从根节点开始递归翻转其左子树和右子树def invertTree(self, root: TreeNode) -> TreeNode:if root != None:temp = root.rightroot.right = self.invertTree(root.left)root.left = self.invertTree(temp)return root