Python實(shí)現(xiàn)二叉樹(shù)的常見(jiàn)遍歷操作總結(jié)【7種方法】
本文實(shí)例講述了Python實(shí)現(xiàn)二叉樹(shù)的常見(jiàn)遍歷操作。分享給大家供大家參考,具體如下:
二叉樹(shù)的定義:
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
二叉樹(shù)的前序遍歷
遞歸
def preorder(root,res=[]):
if not root:
return
res.append(root.val)
preorder(root.left,res)
preorder(root.right,res)
return res
迭代
def preorder(root):
res=[]
if not root:
return []
stack=[root]
while stack:
node=stack.pop()
res.append(node.val)
if node.right:
stack.append(node.right)
if node.left:
stack.append(node,left)
return res
二叉樹(shù)的中序遍歷
遞歸
def inorder(root,res=[]):
if not root:
return
inorder(root.left,res)
res.append(root.val)
inorder(root.right,res)
return res
迭代
def inorder(root):
stack=[]
node=root
res=[]
while stack or node:
while node:
stack.append(node)
node=node.left
node=stack.pop()
res.append(node.val)
node=node.right
return res
二叉樹(shù)的后序遍歷
遞歸
def laorder(root,res=[]):
if not root:
return
laorder(root.left,res)
laorder(root.right,res)
res.append(root.val)
return res
迭代
def laorder(root):
stack=[root]
res=[]
while stack:
node=stack.pop()
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
res.append(node.val)
return res[::-1]
二叉樹(shù)的層次遍歷
迭代
def levelorder(root):
queue=[root]
res=[]
while queue:
node=queue.pop(0)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
res.append(node.val)
return res
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python數(shù)據(jù)結(jié)構(gòu)之二叉樹(shù)的遍歷實(shí)例
- Python利用前序和中序遍歷結(jié)果重建二叉樹(shù)的方法
- python二叉樹(shù)遍歷的實(shí)現(xiàn)方法
- Python實(shí)現(xiàn)二叉樹(shù)結(jié)構(gòu)與進(jìn)行二叉樹(shù)遍歷的方法詳解
- python實(shí)現(xiàn)的二叉樹(shù)定義與遍歷算法實(shí)例
- Python編程實(shí)現(xiàn)二叉樹(shù)及七種遍歷方法詳解
- Python實(shí)現(xiàn)輸入二叉樹(shù)的先序和中序遍歷,再輸出后序遍歷操作示例
- Python實(shí)現(xiàn)二叉樹(shù)前序、中序、后序及層次遍歷示例代碼
- python先序遍歷二叉樹(shù)問(wèn)題
- python創(chuàng)建與遍歷二叉樹(shù)的方法實(shí)例
相關(guān)文章
Python通過(guò)兩個(gè)dataframe用for循環(huán)求笛卡爾積
這篇文章主要介紹了Python通過(guò)兩個(gè)dataframe用for循環(huán)求笛卡爾積,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
TensorFlow2.X使用圖片制作簡(jiǎn)單的數(shù)據(jù)集訓(xùn)練模型
這篇文章主要介紹了TensorFlow2.X使用圖片制作簡(jiǎn)單的數(shù)據(jù)集訓(xùn)練模型,本文通過(guò)截圖實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
numpy求矩陣的特征值與特征向量(np.linalg.eig函數(shù)用法)
這篇文章主要介紹了numpy求矩陣的特征值與特征向量(np.linalg.eig函數(shù)用法),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
python使用if語(yǔ)句實(shí)現(xiàn)一個(gè)猜拳游戲詳解
這篇文章主要介紹了python使用if語(yǔ)句實(shí)現(xiàn)一個(gè)猜拳游戲詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Python中使用tarfile壓縮、解壓tar歸檔文件示例
這篇文章主要介紹了Python中使用tarfile壓縮、解壓tar歸檔文件示例,本文直接給出解壓和壓縮代碼示例,需要的朋友可以參考下2015-04-04
Python?pycharm提交代碼遇到?jīng)_突解決方法
這篇文章主要介紹了Python?pycharm提交代碼遇到?jīng)_突解決方法,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
關(guān)于探究python中sys.argv時(shí)遇到的問(wèn)題詳解
這篇文章主要給大家介紹了python里sys.argv時(shí)遇到問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02

