题目描述:
操作给定的二叉树,将其变换为源二叉树的镜像。
首先对函数的输入参数root结点进行判断,是否是空结点,如果是空则返回None。
接着利用最简单的交换规则,利用a、b临时存储left、right,实现左右子树的交换。
最后分别对左子树和右子树进行递归,交换子树的左右子树。
class Solution:
# 返回镜像树的根节点
def Mirror(self, root):
# write code here
if root==None:
return None
a=root.left
b=root.right
root.left=b
root.right=a
root.left=self.Mirror(root.left)
root.right=self.Mirror(root.right)
return root