欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python?遞歸式實現二叉樹前序,中序,后序遍歷

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

記憶點:

  • 前序: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.結果

6.補充

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

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

相關文章

  • Python自動化辦公之郵件發(fā)送全過程詳解

    Python自動化辦公之郵件發(fā)送全過程詳解

    這篇文章主要介紹了Python自動化辦公之郵件發(fā)送全過程詳解,使用Python實現自動化郵件發(fā)送,可以讓你擺脫繁瑣的重復性業(yè)務,可以節(jié)省非常多的時,下面我們就來看看具體的操作配置吧
    2022-01-01
  • numpy數組切片的使用

    numpy數組切片的使用

    本文主要介紹了numpy數組切片的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • Python數據處理篇之Sympy系列(五)---解方程

    Python數據處理篇之Sympy系列(五)---解方程

    這篇文章主要介紹了Python數據處理篇之Sympy系列(五)---解方程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-10-10
  • python正則表達式(re模塊)的使用詳解

    python正則表達式(re模塊)的使用詳解

    正則表達式是用來匹配字符串非常強大的工具,在其他編程語言中同樣有正則表達式的概念,Python同樣不例外,下面這篇文章主要給大家介紹了關于python正則表達式(re模塊)使用的相關資料,需要的朋友可以參考下
    2022-03-03
  • Pytorch數據讀取與預處理該如何實現

    Pytorch數據讀取與預處理該如何實現

    這篇文章主要介紹了Pytorch數據讀取與預處理該如何實現,幫助大家更好的理解和學習使用Pytorch,感興趣的朋友可以了解下
    2021-03-03
  • 關于Django顯示時間你應該知道的一些問題

    關于Django顯示時間你應該知道的一些問題

    將Django項目部署到Linux系統(tǒng)上進行測試時,發(fā)現操作記錄的時間與服務器的時間不一致,相差13個小時。這主要是因為時區(qū)的問題,下面這篇文章主要總結介紹了關于Django顯示時間你應該知道的一些問題,需要的朋友可以參考下。
    2017-12-12
  • python rsync服務器之間文件夾同步腳本

    python rsync服務器之間文件夾同步腳本

    這篇文章主要為大家詳細介紹了python rsync服務器之間文件夾同步腳本,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Python Web靜態(tài)服務器非堵塞模式實現方法示例

    Python Web靜態(tài)服務器非堵塞模式實現方法示例

    這篇文章主要介紹了Python Web靜態(tài)服務器非堵塞模式實現方法,結合實例形式分析了Python單進程非堵塞模式實現的Web靜態(tài)服務器相關操作技巧,需要的朋友可以參考下
    2019-11-11
  • LyScript實現對內存堆棧掃描的方法詳解

    LyScript實現對內存堆棧掃描的方法詳解

    LyScript插件中提供了三種基本的堆棧操作方法,其中push_stack用于入棧,pop_stack用于出棧,peek_stac可用于檢查指定堆棧位置處的內存參數。所以本文將利用這一特性實現對內存堆棧掃描,感興趣的可以了解一下
    2022-08-08
  • PyTorch加載預訓練模型實例(pretrained)

    PyTorch加載預訓練模型實例(pretrained)

    今天小編就為大家分享一篇PyTorch加載預訓練模型實例(pretrained),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01

最新評論