網站首頁 編程語言 正文
記憶點:
- 前序: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 # 用列表遞歸創建二叉樹 def createTree(root,list_n,i): ?? ?if i
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
原文鏈接:https://blog.csdn.net/weixin_44241793/article/details/123280376
相關推薦
- 2024-04-08 MAC更新和使用composer
- 2023-04-22 python統計函數被調用次數的實現_python
- 2022-04-09 python的函數參數你了解嗎_python
- 2023-04-20 ES6:字符串的擴展及新增方法
- 2022-10-14 CompletableFuture解決多線程返回結果問題
- 2022-11-01 Python正則表達中re模塊的使用_python
- 2022-03-24 .Net?Core微服務網關Ocelot超時、熔斷、限流_自學過程
- 2022-07-13 求兩個降序單鏈表的并集(利用原有鏈點)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支