python实现二叉树的镜像
生活随笔
收集整理的這篇文章主要介紹了
python实现二叉树的镜像
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
操作給定的二叉樹,將其變換為源二叉樹的鏡像。輸入描述:
二叉樹的鏡像定義:源二叉樹 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 hereif not root:returnif root.left is None and root.right == None:returnroot.left,root.right = root.right,root.leftself.Mirror(root.left)self.Mirror(root.right)?
轉載于:https://www.cnblogs.com/tianqizhi/p/9683618.html
總結
以上是生活随笔為你收集整理的python实现二叉树的镜像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 3861 The King’s
- 下一篇: codeforces 1039B Sub