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
您可能感兴趣
- pythonturtle库画图代码(用Python中的turtle模块画图两只小羊方法)
- python numpy矩阵详解(基于Numpy.convolve使用Python实现滑动平均滤波的思路详解)
- python如何安装requests模块(Python常用模块之requests模块用法分析)
- python中for语句的无限循环(python使用for循环计算0-100的整数的和方法)
- python批量图像换背景(详解Python给照片换底色蓝底换红底)
- pythonselenium接口自动测试(python3+selenium自动化测试框架详解)
- python数值基本运算方法(Python常见数字运算操作实例小结)
- python递归深度遍历多叉树(Python实现二叉树的常见遍历操作总结7种方法)
- python 常用爬虫库(Python常用爬虫代码总结方便查询)
- python中startswith使用教程(Python os.access用法实例)
- python中的insert(python assert的用处示例详解)
- python 获取天气预报(详解用python自制微信机器人,定时发送天气预报)
- python创建pdf(Python玩转PDF的各种骚操作)
- python爬虫音乐代码(详解python selenium 爬取网易云音乐歌单名)
- java入坑rabbitmq(Python操作rabbitMQ的示例代码)
- python 微信二维码接口(python实现微信防撤回神器)
- Google 推出了一个游戏生成器,让不会编程的你也能自己设计游戏(推出了一个游戏生成器)
- 二胎家庭老大爱闹情绪,用这招很有效(二胎家庭老大爱闹情绪)
- 一个30岁男人外遇失败的全过程(一个30岁男人外遇失败的全过程)
- 《无敌破坏王2》 不聊彩蛋,聊聊我从动画里看到的现实那些事儿(无敌破坏王2不聊彩蛋)
- 《寄生虫》 三观不正 人类悲欢从来不相通,感同身受也并非本能(寄生虫三观不正)
- 这部动漫中的女孩子,可比101女孩更加励志(这部动漫中的女孩子)
热门推荐
- iframe跨域原理(详解使用postMessage解决iframe跨域通信问题)
- php面向对象怎么用(PHP面向对象程序设计之对象克隆clone和魔术方法__clone用法分析)
- python怎么导入beautifulsoup元素(python使用BeautifulSoup与正则表达式爬取时光网不同地区top100电影并对比)
- dedecms列表栏目样式修改(在dedecms文章页中获取当前栏目字段如content字段)
- html5课程入门(萌新的HTML5 入门指南)
- python比go语言简单(Python和Go语言的区别总结)
- mysql日期与时间函数(MySQL日期函数与时间函数汇总MySQL 5.X)
- 查看SQL SERVER指定数据库中所有的触发器
- mysql千万级别数据查询优化(mysql千万级数据量根据索引优化查询速度的实现)
- dede织梦的后台如何调整和使用(dedecms织梦模板去掉文档内链自动连接关键词的下划线)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9