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

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

    OIS的使用有两种模式:缓冲模式和非缓冲模式.非缓冲方式能处理鼠标或键盘长按的事件,实时性强缓冲方式则不能长按事件,用于非实时响应. 非缓冲输入:1.创建方式:其中 false 参数代表使用非缓冲模式 ...

  2. 在chrome 总调试cordova出现Detached from the target. Remote debugging has been terminated with reason: Connection lost. Please re-attach to the new target

    在chrome 总调试cordova出现如下错误: "Detached from the target. Remote debugging has been terminated with ...

  3. 启用https协议的方法

    提醒:启用https协议会降低服务器性能,如非必要不必启用 一.用openssl生成密钥.证书: 1.生成RSA密钥的方法 openssl genrsa -out privkey.pem 2048 建 ...

  4. 跨域请求解决方法(JSONP, CORS)

    1.跨域 假设我们页面或者应用部署在 http://www.aaa.com 上了,而我们打算从 http://www.bbb.com 请求提取数据.一般情况下,如果我们直接使用 AJAX 来请求将会失 ...

  5. iscsi与multipath

    2016-10-01/21:07:24 http://www.cnblogs.com/wuchanming/p/4019660.htmlhttp://czmmiao.iteye.com/blog/20 ...

  6. [Maven] - 安装与Eclipse搭建

    Maven的具体参考书可以看:<Maven实战> 下载maven可以到:http://maven.apache.org/ Maven的eclipse基本使用可以在这里看到:http://w ...

  7. LeetCode "Third Maximum Number"

    Straight-forward strategy.. please take care of all details - data type, assignment order etc. class ...

  8. Debian MySQL 卸载和安装 PHP安装

    如果是apt-get安装的 sudo apt-get --purge remove mysql-serversudo apt-get --purge remove mysql-clientsudo a ...

  9. 压力测试 php-fpm 优化

    webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便. 1.适用系统:Linux 2.编译安装:引用wget http: ...

  10. 理解Condition的用法

    这个示例中BoundedBuffer是一个固定长度的集合,这个在其put操作时,如果发现长度已经达到最大长度,那么会等待notFull信号,如果得到notFull信号会像集合中添加元素,并发出notE ...