溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何將python二叉樹轉(zhuǎn)換成二叉樹鏡像

發(fā)布時(shí)間:2021-12-13 16:57:39 來源:億速云 閱讀:330 作者:柒染 欄目:大數(shù)據(jù)

本篇文章給大家分享的是有關(guān)如何將python二叉樹轉(zhuǎn)換成二叉樹鏡像,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

二叉樹鏡像

題目

操作給定的二叉樹,將其變換為源二叉樹的鏡像。

思路

  • 先遍歷, 節(jié)點(diǎn)入棧, 再依次出棧調(diào)換左右節(jié)點(diǎn)

  • 遍歷的過程中調(diào)換左右節(jié)點(diǎn)

代碼

# -*- coding:utf-8 -*-
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 先遍歷, 入棧, 再調(diào)換左右節(jié)點(diǎn)
    def Mirror(self, root):
        # 判斷傳入節(jié)點(diǎn)是否為空
        if root is None:
            return None
        line_node = self.printLevelNode(root)
        # print(line_node)
        while line_node:
            tmp = line_node.pop()
            if tmp.left or tmp.right:
                tmp.left, tmp.right = tmp.right, tmp.left
        return tmp
    # 層次遍歷二叉樹, 被調(diào)用
    def printLevelNode(self, root):
        line_node = []
        res = []
        line_node.append(root)
        while line_node:
            tmp = line_node.pop(0)
            res.append(tmp)
            if tmp.left:
                line_node.append(tmp.left)
            if tmp.right:
                line_node.append(tmp.right)
        return res

    # 層次遍歷的過程中調(diào)換左右節(jié)點(diǎn)
    def Mirror2(self, root):
        if root is None:
            return None
        line_node = []
        line_node.append(root)
        while line_node:
            tmp = line_node.pop(0)
            if tmp.left:
                line_node.append(tmp.left)
            if tmp.right:
                line_node.append(tmp.right)
            # if tmp.left or tmp.right:
            tmp.left, tmp.right = tmp.right, tmp.left
        return root

    # 遞歸遍歷的過程中調(diào)換左右節(jié)點(diǎn)
    def Mirror3(self, root):
        if root is None:
            return None
        root.left, root.right = root.right, root.left
        self.Mirror3(root.left)
        self.Mirror3(root.right)
        return root


if __name__ == '__main__':
    node1 = TreeNode(8)
    node2 = TreeNode(6)
    node3 = TreeNode(10)
    node4 = TreeNode(5)
    node5 = TreeNode(7)
    node6 = TreeNode(9)
    node7 = TreeNode(11)
    node1.left = node2
    node1.right = node3
    node2.left = node4
    node2.right = node5
    node3.left = node6
    node3.right = node7
    sl = Solution()
    ls = sl.Mirror3(node1)
    print(ls)
    for i in sl.printLevelNode(ls):
        print(i.val)

以上就是如何將python二叉樹轉(zhuǎn)換成二叉樹鏡像,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI