最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python?遞歸式實(shí)現(xiàn)二叉樹(shù)前序,中序,后序遍歷

 更新時(shí)間:2022年03月07日 16:39:58   作者:步木木  
這篇文章主要介紹了Python?遞歸式實(shí)現(xiàn)二叉樹(shù)前序,中序,后序遍歷,更多相關(guān)資料,需要的小伙伴可以參考下面具體的文章內(nèi)容

記憶點(diǎn):

  • 前序:VLR
  • 中序:LVR
  • 后序:LRV

舉例:

一顆二叉樹(shù)如下圖所示:

則它的前序、中序、后序遍歷流程如下圖所示:

1.前序遍歷

class Solution:
? ? def preorderTraversal(self, root: TreeNode):
? ??
? ? ? ? def preorder(root: TreeNode):
? ? ? ? ? ? if not root:
? ? ? ? ? ? ? ? return
? ? ? ? ? ? res.append(root.val)
? ? ? ? ? ? preorder(root.left) ? ? ? ? ? ?
? ? ? ? ? ? preorder(root.right)
? ? ? ? ? ??
? ? ? ? res = []
? ? ? ? preorder(root)
? ? ? ? return res

2.中序遍歷

class Solution:
? ? def inorderTraversal(self, root: TreeNode):
?? ??? ?
? ? ? ? def inorder(root: TreeNode):
? ? ? ? ? ? if not root:
? ? ? ? ? ? ? ? return
? ? ? ? ? ? inorder(root.left)
? ? ? ? ? ? res.append(root.val)
? ? ? ? ? ? inorder(root.right)
? ? ? ??
? ? ? ? res = []
? ? ? ? inorder(root)
? ? ? ? return res

3.后序遍歷

class Solution:
? ? def postorderTraversal(self, root: TreeNode):
?? ??? ?
? ? ? ? def postorder(root: TreeNode):
? ? ? ? ? ? if not root:
? ? ? ? ? ? ? ? return
? ? ? ? ? ? postorder(root.left)
? ? ? ? ? ? res.append(root.val)
? ? ? ? ? ? postorder(root.right)
? ? ? ??
? ? ? ? res = []
? ? ? ? postorder(root)
? ? ? ? return res

4.測(cè)試

class TreeNode:
?? ?def __init__(self, val=0, left=None, right=None):
?? ??? ?self.val = val
?? ??? ?self.left = left
?? ??? ?self.right = right

# 用列表遞歸創(chuàng)建二叉樹(shù)
def createTree(root,list_n,i):
?? ?if i <len(list_n):
?? ??? ?if list_n[i] == 'null':
?? ??? ??? ??? ?return None
?? ??? ?else:
?? ??? ??? ?root = TreeNode(val = list_n[i])
?? ??? ??? ?root.left = createTree(root.left,list_n,2*i+1)
?? ??? ??? ?root.right = createTree(root.right,list_n,2*i+2)
?? ??? ??? ?return root ?
?? ??? ?return root
?? ??? ?
class Solution:
?? ?def preorderTraversal(self, root: TreeNode): # 前序
?? ?
?? ??? ?def preorder(root: TreeNode):
?? ??? ??? ?if not root:
?? ??? ??? ??? ?return
?? ??? ??? ?res.append(root.val)
?? ??? ??? ?preorder(root.left) ? ? ? ? ? ?
?? ??? ??? ?preorder(root.right)
?? ??? ??? ?
?? ??? ?res = []
?? ??? ?preorder(root)
?? ??? ?return res

?? ?def inorderTraversal(self, root: TreeNode): # 中序
?? ?
?? ??? ?def inorder(root: TreeNode):
?? ??? ??? ?if not root:
?? ??? ??? ??? ?return
?? ??? ??? ?inorder(root.left)
?? ??? ??? ?res.append(root.val)
?? ??? ??? ?inorder(root.right)
?? ??? ??? ?
?? ??? ?res = []
?? ??? ?inorder(root)
?? ??? ?return res
?? ??? ?
?? ?def postorderTraversal(self, root: TreeNode): # 后序
?? ?
?? ??? ?def postorder(root: TreeNode):
?? ??? ??? ?if not root:
?? ??? ??? ??? ?return
?? ??? ??? ?postorder(root.left)
?? ??? ??? ?postorder(root.right)
?? ??? ??? ?res.append(root.val)
?? ??? ??? ?
?? ??? ?res = []
?? ??? ?postorder(root)
?? ??? ?return res

if __name__ == "__main__":

?? ?root = TreeNode()
?? ?list_n = [1,2,3,4,5,6,7,8,'null',9,10]
?? ?root = createTree(root,list_n,0)
?? ?s = Solution()
?? ?res_pre = s.preorderTraversal(root)
?? ?res_in = s.inorderTraversal(root)
?? ?res_post = s.postorderTraversal(root)
?? ?print(res_pre)
?? ?print(res_in)
?? ?print(res_post)

5.結(jié)果

6.補(bǔ)充

6.1N叉樹(shù)前序遍歷

"""
# Definition for a Node.
class Node:
? ? def __init__(self, val=None, children=None):
? ? ? ? self.val = val
? ? ? ? self.children = children
"""

class Solution:
? ? def postorder(self, root: 'Node') -> List[int]:
? ? ? ? def seq(root):
? ? ? ? ? ? if not root:
? ? ? ? ? ? ? ? return
? ? ? ? ? ? res.append(root.val)
? ? ? ? ? ? for child in root.children:
? ? ? ? ? ? ? ? seq(child) ? ? ? ? ? ?
? ? ? ? res = []
? ? ? ? seq(root)
? ? ? ? return res

N叉樹(shù)后序遍歷
"""
# Definition for a Node.
class Node:
? ? def __init__(self, val=None, children=None):
? ? ? ? self.val = val
? ? ? ? self.children = children
"""

class Solution:
? ? def postorder(self, root: 'Node') -> List[int]:
? ? ? ? def seq(root):
? ? ? ? ? ? if not root:
? ? ? ? ? ? ? ? return
? ? ? ? ? ? for child in root.children:
? ? ? ? ? ? ? ? seq(child)
? ? ? ? ? ? res.append(root.val)
? ? ? ? res = []
? ? ? ? seq(root)
? ? ? ? return res

到此這篇關(guān)于Python 遞歸式實(shí)現(xiàn)二叉樹(shù)前序,中序,后序遍歷的文章就介紹到這了,更多相關(guān)二叉樹(shù)前序,中序,后序遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python 虛擬環(huán)境的創(chuàng)建與使用方法

    python 虛擬環(huán)境的創(chuàng)建與使用方法

    本文先介紹虛擬環(huán)境的基礎(chǔ)知識(shí)以及使用方法,然后再深入介紹虛擬環(huán)境背后的工作原理,需要的朋友可以參考下
    2021-06-06
  • python 實(shí)現(xiàn)文件的遞歸拷貝實(shí)現(xiàn)代碼

    python 實(shí)現(xiàn)文件的遞歸拷貝實(shí)現(xiàn)代碼

    今天翻電腦時(shí)突然發(fā)現(xiàn)有個(gè)存了很多照片和視頻的文件夾,想起來(lái)是去年換手機(jī)(流行的小5)時(shí)拷出來(lái)的??戳藥讖堈掌掠忠荒荒坏母‖F(xiàn)在腦海,好吧,我是個(gè)感性的人
    2012-08-08
  • Python+Django實(shí)現(xiàn)接口測(cè)試工具的示例代嗎

    Python+Django實(shí)現(xiàn)接口測(cè)試工具的示例代嗎

    本文主要介紹了Python+Django實(shí)現(xiàn)接口測(cè)試工具,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • python語(yǔ)音識(shí)別的轉(zhuǎn)換方法

    python語(yǔ)音識(shí)別的轉(zhuǎn)換方法

    語(yǔ)音識(shí)別技術(shù),也被稱為自動(dòng)語(yǔ)音識(shí)別,目標(biāo)是以電腦自動(dòng)將人類的語(yǔ)音內(nèi)容轉(zhuǎn)換為相應(yīng)的文字。應(yīng)用包括語(yǔ)音撥號(hào)、語(yǔ)音導(dǎo)航、室內(nèi)設(shè)備控制、語(yǔ)音文檔檢索、簡(jiǎn)單的聽(tīng)寫(xiě)數(shù)據(jù)錄入等。本文給大家介紹python語(yǔ)音識(shí)別的方法,感興趣的朋友一起看看吧
    2021-10-10
  • 用Python實(shí)現(xiàn)斐波那契(Fibonacci)函數(shù)

    用Python實(shí)現(xiàn)斐波那契(Fibonacci)函數(shù)

    這篇文章主要介紹了用Python實(shí)現(xiàn)斐波那契(Fibonacci)函數(shù)的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • jupyter notebook參數(shù)化運(yùn)行python方式

    jupyter notebook參數(shù)化運(yùn)行python方式

    這篇文章主要介紹了jupyter notebook參數(shù)化運(yùn)行python方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • 詳解Python調(diào)用系統(tǒng)命令的六種方法

    詳解Python調(diào)用系統(tǒng)命令的六種方法

    這篇文章主要介紹了詳解Python調(diào)用系統(tǒng)命令的六種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Python爬蟲(chóng)實(shí)例之2021貓眼票房字體加密反爬策略(粗略版)

    Python爬蟲(chóng)實(shí)例之2021貓眼票房字體加密反爬策略(粗略版)

    這篇文章主要介紹了Python爬蟲(chóng)實(shí)例之2021貓眼票房字體加密反爬策略(粗略版),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • 15個(gè)最近才知道的Python實(shí)用操作

    15個(gè)最近才知道的Python實(shí)用操作

    這篇文章主要和大家分享了15個(gè)最近才知道的Python實(shí)用操作,文中的示例代碼講解詳細(xì),對(duì)我們深入了解Python有一定的幫助,感興趣的小伙伴可以了解一下
    2023-04-04
  • 利用python獲得時(shí)間的實(shí)例說(shuō)明

    利用python獲得時(shí)間的實(shí)例說(shuō)明

    在python中,它的time模塊功能十分強(qiáng)大,我們今天就來(lái)學(xué)習(xí)下,廢話少說(shuō),我們來(lái)看下實(shí)際的效果,下面貼出代碼:
    2013-03-03

最新評(píng)論

泽库县| 额尔古纳市| 瓮安县| 科尔| 博罗县| 湾仔区| 启东市| 遂宁市| 洪洞县| 兴国县| 老河口市| 吐鲁番市| 辽阳县| 龙山县| 丹巴县| 汉沽区| 长阳| 饶阳县| 民县| 宣恩县| 南岸区| 新竹市| 盖州市| 奈曼旗| 尉犁县| 青海省| 湾仔区| 大新县| 武冈市| 绥芬河市| 黎城县| 揭西县| 宝丰县| 丰镇市| 九龙县| 雷波县| 富阳市| 大姚县| 泰州市| 安西县| 七台河市|