二叉树遍历(Binary Tree Traversal)
二叉树的递归遍历比较简单,这里说一下非递归遍历,以中序遍历为例子。
非递归遍历主要用到栈来协助进行。对于一个二叉树,首先根节点入栈,如果有左儿子,则继续入栈,重复直到最左边的儿子,这时候此节点值为要遍历的第一个值,他父亲是在栈顶。所以我们做一次出栈操作 f = stack.pop(),并将 f.val 值存为第二个点,接下来要遍历 f.right,完成后栈的最后元素是 f 的父亲,继续做出栈操作。重复下去可以完成遍历。
def inorderTraversal(self, root):
res = []
stack = []
while root!=None or stack!=[]:
while root!=None:
stack.append(root)
root = root.left
if stack!=[]:
k = stack.pop()
res.append(k.val)
root = k.right
return res
二叉树遍历(Binary Tree Traversal)的更多相关文章
- LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化
遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 数据结构-二叉树(Binary Tree)
1.二叉树(Binary Tree) 是n(n>=0)个结点的有限集合,该集合或者为空集(空二叉树),或者由一个根节点和两棵互不相交的,分别称为根节点的左子树和右子树的二叉树组成. 2.特数二 ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- [Swift]LeetCode94. 二叉树的中序遍历 | Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- [Swift]LeetCode102. 二叉树的层次遍历 | Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [Swift]LeetCode103. 二叉树的锯齿形层次遍历 | Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- [java]wordcount程序
词数统计系统. 作业解析:这次作业的内容是从本地读取一个程序代码,计算出这个程序中的行数,单词数,也可进行拓展. 实现语言:java 编程思路: 程序是由各种单词和符号组成的,单词包括关键字,标识符这 ...
- Windows环境下npm install常见错误
Windows环境下npm install安装包依赖时,常出现一些错误,下面为个人解决办法: 错误一 缺少python环境: G:\nodejs\moviesite\node_modules\bcry ...
- Python range() xrange()
range 前面小节已经说明了,range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个序列. xrange用法与 range 完全 ...
- java 8 原版 api 下载地址,
http://download.oracle.com/otn-pub/java/javafx/8.0.25-b17/javafx-8u25-apidocs.zip?AuthParam=14174994 ...
- 【python】pickle模块
持久性的基本思想很简单.假定有一个 Python 程序,它可能是一个管理日常待办事项的程序,您希望在多次执行这个程序之间可以保存应用程序对象(待办事项).换句话说,您希望将对象存储在磁盘上,便于以后检 ...
- 北京动软VAR团队的HoloLens开发教程最新搜罗整理
日前,微软为Windows开发者带来Win10版HoloLens全息眼镜模拟器SDK开发套件工具,借助最新发布的VS2015 Update2和Win10 SDK工具,直接在PC平台上开发和调试原生Wi ...
- SQLServer2005删除log文件和清空日志的方案
数据库在使用过程中会使日志文件不断增加,使得数据库的性能下降,并且占用大量的磁盘空间.SQL Server数据库都有log文件,log文件记录用户对数据库修改的操作.可以通过直接删除log文件和清空日 ...
- 结算凭证中委托付款部分sql
select a.makevdate,a.summary,a.totalcredit,a.cent_typeid,c.accidname from fts_voucher a,fts_voucher_ ...
- Easyui 小脚本
function addTab(subtitle, url, icon) { if (!$('#tabs').tabs('exists', subtitle)) { $('#tabs').tabs(' ...
- Eclipse插件推荐
1.Eclipse颜色插件 https://github.com/eclipse-color-theme/eclipse-color-theme 2.google Code Analysis http ...