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.測試
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程
這篇文章主要介紹了Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10Pytorch數(shù)據(jù)讀取與預(yù)處理該如何實(shí)現(xiàn)
這篇文章主要介紹了Pytorch數(shù)據(jù)讀取與預(yù)處理該如何實(shí)現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用Pytorch,感興趣的朋友可以了解下2021-03-03關(guān)于Django顯示時(shí)間你應(yīng)該知道的一些問題
將Django項(xiàng)目部署到Linux系統(tǒng)上進(jìn)行測試時(shí),發(fā)現(xiàn)操作記錄的時(shí)間與服務(wù)器的時(shí)間不一致,相差13個(gè)小時(shí)。這主要是因?yàn)闀r(shí)區(qū)的問題,下面這篇文章主要總結(jié)介紹了關(guān)于Django顯示時(shí)間你應(yīng)該知道的一些問題,需要的朋友可以參考下。2017-12-12Python Web靜態(tài)服務(wù)器非堵塞模式實(shí)現(xiàn)方法示例
這篇文章主要介紹了Python Web靜態(tài)服務(wù)器非堵塞模式實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Python單進(jìn)程非堵塞模式實(shí)現(xiàn)的Web靜態(tài)服務(wù)器相關(guān)操作技巧,需要的朋友可以參考下2019-11-11LyScript實(shí)現(xiàn)對內(nèi)存堆棧掃描的方法詳解
LyScript插件中提供了三種基本的堆棧操作方法,其中push_stack用于入棧,pop_stack用于出棧,peek_stac可用于檢查指定堆棧位置處的內(nèi)存參數(shù)。所以本文將利用這一特性實(shí)現(xiàn)對內(nèi)存堆棧掃描,感興趣的可以了解一下2022-08-08PyTorch加載預(yù)訓(xùn)練模型實(shí)例(pretrained)
今天小編就為大家分享一篇PyTorch加載預(yù)訓(xùn)練模型實(shí)例(pretrained),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01