python递归深度遍历多叉树(Python实现二叉树的常见遍历操作总结7种方法)
类别:脚本大全 浏览量:576
时间:2022-01-14 02:53:52 python递归深度遍历多叉树
Python实现二叉树的常见遍历操作总结7种方法本文实例讲述了Python实现二叉树的常见遍历操作。分享给大家供大家参考,具体如下:
二叉树的定义:
|
class TreeNode: def __init__( self , x): self .val = x self .left = None self .right = None |
二叉树的前序遍历
递归
|
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 |
二叉树的中序遍历
递归
|
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 |
二叉树的后序遍历
递归
|
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 ] |
二叉树的层次遍历
迭代
|
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 |
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/wenkenza5368/article/details/79573333
您可能感兴趣
- python操作sql server数据库(Python 数据库操作 SQLAlchemy的示例代码)
- 超简单使用Python换脸实例(超简单使用Python换脸实例)
- python获取系统的utc时间(Python的UTC时间转换讲解)
- python入门知识点总结(深入解析Python小白学习操作列表)
- python写一个二叉树(Python二叉搜索树与双向链表转换算法示例)
- python发送钉钉消息(钉钉群自定义机器人消息Python封装的实例)
- python网络爬虫案例实战(python爬取cnvd漏洞库信息的实例)
- python 爬虫招聘(Python3获取拉勾网招聘信息的方法实例)
- python创建pdf(Python玩转PDF的各种骚操作)
- python里面的time如何用(详解python:time模块用法)
- python停止执行的代码(python定时检测无响应进程并重启的实例代码)
- python多进程创建细节(Python实现多进程的四种方式)
- python创建hbase命名空间(python使用phoenixdb操作hbase的方法示例)
- 怎样在python中散点图(python使用Plotly绘图工具绘制散点图、线形图)
- python爬取豆瓣评分排行榜(Python爬虫——爬取豆瓣电影Top250代码实例)
- python自动化邮件管理(Python实现E-Mail收集插件实例教程)
- 每天都吃水果的好处(每天吃水果的好处与功效)
- 苹果15价格(苹果15价格512g官网)
- 春节放假几天(春节放假几天2023法定几天)
- 今天 3月13日,31年前,一个英雄少年感动了中国(今天3月13日31年前)
- 你好,新成理人丨成都理工大学2019级新生开学典礼隆重举行(新成理人丨成都理工大学2019级新生开学典礼隆重举行)
- 这部民警编演的红色话剧,讲述了一个不断追寻的故事(这部民警编演的红色话剧)
热门推荐
- dedecms标签怎么调用(dedecms网站栏目增加缩略图的方法)
- laravel框架入门项目(在laravel框架中使用model层的方法)
- laravel开发api实例(Laravel框架集成UEditor编辑器的方法图文与实例详解)
- centos6.5 32位安装gitlab(centos7安装部署gitlab服务器的方法)
- iis警告事件怎么处理(IIS服务器常见错误解析及排错指南、错误代码大全)
- 云主机和虚拟主机的区别(虚拟主机和云服务器有什么区别)
- python strip用法(Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析)
- pandas如何指定空数据类型(解决Pandas的DataFrame输出截断和省略的问题)
- python参模块使用教程(Python参数解析模块sys、getopt、argparse使用与对比分析)
- centos中安装sql图(CentOS 7.3上SQL Server vNext CTP 1.2安装教程)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9