Python?遞歸式實(shí)現(xiàn)二叉樹前序,中序,后序遍歷
記憶點(diǎn):

- 前序:VLR
- 中序:LVR
- 后序:LRV
舉例:
一顆二叉樹如下圖所示:

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

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)建二叉樹 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叉樹前序遍歷
""" # 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叉樹后序遍歷 """ # 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)二叉樹前序,中序,后序遍歷的文章就介紹到這了,更多相關(guān)二叉樹前序,中序,后序遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 虛擬環(huán)境的創(chuàng)建與使用方法
本文先介紹虛擬環(huán)境的基礎(chǔ)知識(shí)以及使用方法,然后再深入介紹虛擬環(huán)境背后的工作原理,需要的朋友可以參考下2021-06-06
python 實(shí)現(xiàn)文件的遞歸拷貝實(shí)現(xiàn)代碼
今天翻電腦時(shí)突然發(fā)現(xiàn)有個(gè)存了很多照片和視頻的文件夾,想起來(lái)是去年換手機(jī)(流行的小5)時(shí)拷出來(lái)的??戳藥讖堈掌?,往事又一幕幕的浮現(xiàn)在腦海,好吧,我是個(gè)感性的人2012-08-08
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)換方法
語(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)單的聽寫數(shù)據(jù)錄入等。本文給大家介紹python語(yǔ)音識(shí)別的方法,感興趣的朋友一起看看吧2021-10-10
用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方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
詳解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爬蟲實(shí)例之2021貓眼票房字體加密反爬策略(粗略版)
這篇文章主要介紹了Python爬蟲實(shí)例之2021貓眼票房字體加密反爬策略(粗略版),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
利用python獲得時(shí)間的實(shí)例說(shuō)明
在python中,它的time模塊功能十分強(qiáng)大,我們今天就來(lái)學(xué)習(xí)下,廢話少說(shuō),我們來(lái)看下實(shí)際的效果,下面貼出代碼:2013-03-03

