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 # 用列表递归创建二叉树 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 递归式实现二叉树前序,中序,后序遍历的文章就介绍到这了,更多相关二叉树前序,中序,后序遍历内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
使用scipy.optimize的fsolve,root函数求解非线性方程问题
这篇文章主要介绍了使用scipy.optimize的fsolve,root函数求解非线性方程问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-12-12Python selenium get_cookies获取cookie不全的解决方案
之前使用爬虫时最让我头疼的就是cookie失效的问题了,下面这篇文章主要给大家介绍了关于Python selenium get_cookies获取cookie不全的解决方案,需要的朋友可以参考下2022-10-10Pycharm代码无法复制,无法选中删除,无法编辑的解决方法
今天小编就为大家分享一篇Pycharm代码无法复制,无法选中删除,无法编辑的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-10-10python playwright--pytest-playwright、pytest-base-url插件编
这篇文章主要介绍了python playwright--pytest-playwright、pytest-base-url插件编写用例,它提供上下文隔离,开箱即用地在多个浏览器配置上运行,它继承了pytest框架,以及支持playwright的一些基础使用,需要的朋友可以参考下2023-05-05
最新评论