19、二叉树的镜像

代码实现:

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回镜像树的根节点
    def Mirror(self, root):
        # write code here
        if root == None or (root.left == None and root.right == None):
            return

        #根节点不为空,就交换左右子节点,然后对左右子节点接着进行递归
        temp = root.left
        root.left = root.right
        root.right = temp

        self.Mirror(root.left)
        self.Mirror(root.right)

你可能感兴趣的:(19、二叉树的镜像)