Python 递归式实现二叉树前序,中序,后序遍历
作者:步木木 时间:2022-09-22 17:38:32
记忆点:
前序: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 <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
来源:https://blog.csdn.net/weixin_44241793/article/details/123280376
标签:Python,递归式,二叉树,前序,中序,后序,遍历
0
投稿
猜你喜欢
仿谷歌主页js动画效果实现代码
2024-04-17 10:07:36
VBScript GetObject 函数用法介绍
2008-01-30 17:00:00
Python RuntimeError: thread.__init__() not called解决方法
2022-12-22 17:11:46
使用Django xadmin 实现修改时间选择器为不可输入状态
2023-11-19 12:12:06
asp显示左边的n个字符自动识别汉字的函数
2007-09-13 12:16:00
浅谈mysqldump使用方法(MySQL数据库的备份与恢复)
2024-01-20 16:29:23
Python读取配置文件的实战操作
2021-08-12 19:48:09
tornado 多进程模式解析
2021-01-20 10:56:44
Python 删除连续出现的指定字符的实例
2023-11-21 08:36:15
以Python代码实例展示kNN算法的实际运用
2024-05-22 10:28:42
注册和填表中常见的中英文对照
2008-07-26 12:12:00
js实现浏览器倒计时跳转页面效果
2023-07-18 06:49:57
Python正则表达式分组概念与用法详解
2022-02-12 08:41:25
Qt5 实现主窗口状态栏显示时间
2022-05-29 23:54:45
ASP编码问题的深入研究与解决方案(MSDN)
2007-10-25 11:54:00
SQL Server 2019自定义安装教程
2024-01-12 21:14:22
Python绘制散点密度图的三种方式详解
2021-12-07 00:21:04
python基础之并发编程(二)
2023-01-16 03:10:37
基于Python批量生成指定尺寸缩略图代码实例
2021-12-15 14:58:48
系统高吞吐量下的数据库重复写入问题分析解决
2024-01-17 07:37:21