题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。

输入描述:

二叉树的镜像定义:源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5

解题思路

和二叉树相关,肯定要用到递归

  1. # -*- coding:utf-8 -*-
  2. # class TreeNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7. class Solution:
  8. # 返回镜像树的根节点
  9. def Mirror(self, root):
  10. if not root:
  11. return None
  12. root.left,root.right=root.right,root.left
  13. self.Mirror(root.left)
  14. self.Mirror(root.right)
  15. return root