二叉树的递归遍历比较简单,这里说一下非递归遍历,以中序遍历为例子。

非递归遍历主要用到栈来协助进行。对于一个二叉树,首先根节点入栈,如果有左儿子,则继续入栈,重复直到最左边的儿子,这时候此节点值为要遍历的第一个值,他父亲是在栈顶。所以我们做一次出栈操作   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)的更多相关文章

  1. LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

    103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...

  2. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  3. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  4. 数据结构-二叉树(Binary Tree)

    1.二叉树(Binary Tree) 是n(n>=0)个结点的有限集合,该集合或者为空集(空二叉树),或者由一个根节点和两棵互不相交的,分别称为根节点的左子树和右子树的二叉树组成.  2.特数二 ...

  5. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  6. [Swift]LeetCode94. 二叉树的中序遍历 | Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  7. [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, ...

  8. [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 ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. NOIP2016日记

    今天下午2:30~4:30考NOIP2016..我4:00前久出来了,没仔细检查.. 错了两道基础题..(T_T)  >_< 至少能过..就这样吧..努力复赛!!

  2. Codeforces Round #294 (Div. 2) D. A and B and Interesting Substrings

    题意: 对于26个字母 每个字母分别有一个权值 给出一个字符串,找出有多少个满足条件的子串, 条件:1.第一个字母和最后一个相同,2.除了第一个和最后一个字母外,其他的权和为0 思路: 预处理出sum ...

  3. SQL 行转列和列转行2

    DECLARE @T TABLE (columnName varchar(100) NOT NULL PRIMARY KEY); INSERT INTO @T SELECT columnName fr ...

  4. How To: Samba4 AD PDC + Windows XP, Vista and 7

    dnsmasq If you've been struggling with Samba3 domain controllers and NT4 style domains working with ...

  5. 更改localhost默认打开的index.html的地址三步曲

    首先说明,我的Apache安装路径是F:\software installing\Apache2.2 解释一下,localhost默认打开的是安装路径下index.html 也就是路径F:\softw ...

  6. CentOS7关闭防火墙方法

    在之前的版本中关闭防火墙等服务的命令是 service iptables stop /etc/init.d/iptables stop 在RHEL7中,其实没有这个服务 [root@rhel7 ~]# ...

  7. u-boot-2010.09移植(A)

    第一阶段 1.开发环境 系统:centOS6.5           linux版本:2.6.32         交叉编译器:buildroot-2012.08 以上工具已经准备好,具体安装步骤不再 ...

  8. if you end up with a boring miserable life

  9. 【sql】之查询昨天的记录

    http://blog.csdn.net/cangchen/article/details/44978531

  10. [经验交流] docker in docker 的变通实现方法

    最近在做CI持续集成环境的容器化,其中一个工作是:在容器中构建容器镜像. 对于这个需求,网上有一些 Docker in Docker 的方法,具体需要修改宿主机的配置.这种方式在单机环境下.对安全要求 ...