[DS+Algo] 010 二叉树的遍历
二叉树遍历
深度优先
- 一般用递归
- 一些名词
| 遍历方式 | 英文 |
|---|---|
| 先序 | Preorder |
| 中序 | Inorder |
| 后序 | Postorder |
广度优先
- 一般用队列
Python 代码示例
class Node(object):
def __init__(self, elem=-1, lchild=None, rchild=None):
self.elem = elem
self.lchild = lchild
self.rchild = rchild
class Tree(object):
def __init__(self, root=None):
self.root = root
def add(self, elem):
""" 走的是“完全二叉树的路” """
node = Node(elem)
if self.root is None:
self.root = node
else:
queue = []
queue.append(self.root)
while queue:
cur = queue.pop(0)
if cur.lchild is None:
cur.lchild = node
return
elif cur.rchild is None:
cur.rchild = node
return
else:
queue.append(cur.lchild)
queue.append(cur.rchild)
def preorder(self, root):
if root is None:
return
print(root.elem, end=' ')
self.preorder(root.lchild)
self.preorder(root.rchild)
def inorder(self, root):
if root is None:
return
self.inorder(root.lchild)
print(root.elem, end=' ')
self.inorder(root.rchild)
def postorder(self, root):
if root is None:
return
self.postorder(root.lchild)
self.postorder(root.rchild)
print(root.elem, end=' ')
def breath_traverse(self, root):
if root is None:
return
queue = []
queue.append(root)
while queue:
node = queue.pop(0)
print(node.elem, end=' ')
if node.lchild:
queue.append(node.lchild)
if node.rchild:
queue.append(node.rchild)
print()
if __name__ == "__main__":
tree = Tree()
for i in range(10):
tree.add(i)
tree.preorder(tree.root)
print()
tree.inorder(tree.root)
print()
tree.postorder(tree.root)
print()
tree.breath_traverse(tree.root)
[DS+Algo] 010 二叉树的遍历的更多相关文章
- 二叉树的遍历(递归,迭代,Morris遍历)
二叉树的三种遍历方法: 先序,中序,后序,这三种遍历方式每一个都可以用递归,迭代,Morris三种形式实现,其中Morris效率最高,空间复杂度为O(1). 主要参考博客: 二叉树的遍历(递归,迭代, ...
- C++ 二叉树深度优先遍历和广度优先遍历
二叉树的创建代码==>C++ 创建和遍历二叉树 深度优先遍历:是沿着树的深度遍历树的节点,尽可能深的搜索树的分支. //深度优先遍历二叉树void depthFirstSearch(Tree r ...
- 二叉树的遍历(递归,迭代,Morris遍历)
二叉树的遍历: 先序,中序,后序: 二叉树的遍历有三种常见的方法, 最简单的实现就是递归调用, 另外就是飞递归的迭代调用, 最后还有O(1)空间的morris遍历: 二叉树的结构定义: struct ...
- [Leetcode] Binary tree level order traversal二叉树层次遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 算法与数据结构(三) 二叉树的遍历及其线索化(Swift版)
前面两篇博客介绍了线性表的顺序存储与链式存储以及对应的操作,并且还聊了栈与队列的相关内容.本篇博客我们就继续聊数据结构的相关东西,并且所涉及的相关Demo依然使用面向对象语言Swift来表示.本篇博客 ...
- C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解
剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
- python3实现二叉树的遍历与递归算法解析
1.二叉树的三种遍历方式 二叉树有三种遍历方式:先序遍历,中序遍历,后续遍历 即:先中后指的是访问根节点的顺序 eg:先序 根左右 中序 左根右 后序 左右根 遍历总体思路:将树分成最小 ...
- 二叉树的遍历--C#程序举例二叉树的遍历
二叉树的遍历--C#程序举例二叉树的遍历 关于二叉树的介绍笨男孩前面写过一篇博客 二叉树的简单介绍以及二叉树的存储结构 遍历方案 二叉树的遍历分为以下三种: 先序遍历:遍历顺序规则为[根左右] 中序遍 ...
- 数据结构与算法之PHP实现二叉树的遍历
一.二叉树的遍历 以某种特定顺序访问树中所有的节点称为树的遍历,遍历二叉树可分深度优先遍历和广度优先遍历. 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次.可以细分 ...
随机推荐
- 迭代处理enum类会生成枚举的各个成员
import enum class BugStatus(enum.Enum): new = 7 incomplete = 6 invalid = 5 wont_fix ...
- Django对中间件的调用思想、csrf中间件详细介绍、Django settings源码剖析、Django的Auth模块
目录 使用Django对中间件的调用思想完成自己的功能 功能要求 importlib模块介绍 功能的实现 csrf中间件详细介绍 跨站请求伪造 Django csrf中间件 form表单 ajax c ...
- select 语句关键字优先级
1.select 语句优先级 select classid, userid, name, age from study where userid > 15 group by classid ha ...
- linux下简易端口扫描器
#include<iostream> #include<string.h> #include<sys/types.h> #include<sys/socket ...
- Android处理未捕获的异常(应用全局异常)
public class CrashHandler implements UncaughtExceptionHandler { private static CrashHandler instance ...
- 新年第一发--HDU1848--Fibonacci again and again(SG函数)
Problem Description 任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的:F(1)=1;F(2)=2;F(n)=F(n-1)+F(n-2 ...
- Oracle数据库锁表查询
--查看数据库最大连接数 select value from v$parameter where name = 'processes'; --更改数据库连接数 alter system scope = ...
- Hive函数介绍
一些函数不太会,查了些资料,分享一下 Hive已定义函数介绍: 1.字符串长度函数:length 语法: length(string A)返回值: int举例:[sql] view plain cop ...
- wannalfy 挑战赛7 F Masha与老鼠(贪心+dp)
链接:https://www.nowcoder.net/acm/contest/56/F 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 6 ...
- 步步向前之Element-UI
Table 固定表头 只要在el-table元素中定义了height属性,即可实现固定表头的表格,而不需要额外的代码.例如: <el-table :data="tableData3&q ...