題目描述
操作給定的二叉樹,將其變換爲源二叉樹的鏡像。
輸入描述
二叉樹的鏡像定義:源二叉樹
8
/ \
6 10
/ \ / \
5 7 9 11
鏡像二叉樹
8
/ \
10 6
/ \ / \
11 9 7 5
代碼實現
# -*- 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:
root.left,root.right=root.right,root.left#python中交換兩個數的特殊表達方式
self.Mirror(root.left)
self.Mirror(root.right)