There are three types of depth-first traversal: pre-order,in-order, and post-order.

For a binary tree, they are defined as operations recursively at each node, starting with the root node as follows:

Pre-order

Visit the root.
Traverse the left subtree.
Traverse the right subtree.

iterativePreorder(node)
parentStack = empty stack
parentStack.push(null)
top = node
while ( top != null )
visit( top )
if ( top.right != null )
parentStack.push(top.right)
if ( top.left != null )
parentStack.push(top.left)
top = parentStack.pop()

In-order

Traverse the left subtree.
Visit root.
Traverse the right subtree.

iterativeInorder(node)
parentStack = empty stack
while (not parentStack.isEmpty() or node ≠ null)
if (node ≠ null)
parentStack.push(node)
node = node.left
else
node = parentStack.pop()
visit(node)
node = node.right

Post-order

Traverse the left subtree.
Traverse the right subtree.
Visit the root.

iterativePostorder(node)
parentStack = empty stack
lastnodevisited = null
while (not parentStack.isEmpty() or node ≠ null)
if (node ≠ null)
parentStack.push(node)
node = node.left
else
peeknode = parentStack.peek()
if (peeknode.right ≠ null and lastnodevisited ≠ peeknode.right)
/* if right child exists AND traversing node from left child, move right */
node = peeknode.right
else
parentStack.pop()
visit(peeknode)
lastnodevisited = peeknode

Morris Travel

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> ret;
if (!root) return ret;
TreeNode *cur = root; while (cur) {
if (!cur->left) {
ret.push_back(cur->val);
cur = cur->right;
} else {
TreeNode *rightmost = cur->left;
while (rightmost->right != NULL && rightmost->right != cur) rightmost = rightmost->right;
if (rightmost->right == cur) {
rightmost->right = NULL;
ret.push_back(cur->val);
cur = cur->right;
} else {
rightmost->right = cur;
cur = cur->left;
}
}
} return ret;
}
};

后序:

Algorithm | Tree traversal的更多相关文章

  1. Hierarchical Tree Traversal in Graphics Pipeline Stages

    BACKGROUND Many algorithms on a graphics processing unit (GPU) may benefit from doing a query in a h ...

  2. LeetCode: Binary Tree Traversal

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

  3. [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  4. tree traversal

    tree traversal tree 遍历 中序,顺序,左中右 先序,先父节点,中左右 后序,先子节点,左右中 二叉搜索树 "use strict"; /** * * @auth ...

  5. Binary Tree Traversal

    1.Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For ex ...

  6. [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. 【Leetcode】Binary Tree Traversal

    把三个二叉树遍历的题放在一起了. 递归写法太简单,就不再实现了,每题实现了两种非递归算法. 一种是利用栈,时间和空间复杂度都是O(n). 另一种是借助线索二叉树,也叫Morris遍历,充分利用树中节点 ...

  8. Binary Tree Traversal 二叉树的前中后序遍历

    [抄题]:二叉树前序遍历 [思维问题]: 不会递归.三要素:下定义.拆分问题(eg root-root.left).终止条件 [一句话思路]: 节点非空时往左移,否则新取一个点 再往右移. [输入量] ...

  9. [Algorithm] Tree: Lowest Common Ancestor

    By given a tree structure, task is to find lowest common ancestor: For example, LCA(4, 5) --> > ...

随机推荐

  1. css/js(工作中遇到的问题)

    移动设备点击时去掉外加的蓝色边框 a, input, button { -webkit-tap-highlight-color: rgba(0,0,0,0); -webkit-tap-highligh ...

  2. javascript优化--11模式(设计模式)02

    策略模式 在选择最佳策略以处理特定任务(上下文)的时候仍然保持相同的接口: //表单验证的例子 var data = { firs_name: "Super", last_name ...

  3. PDA手持移动POS销售开单软件(网络版)、PDA手持设备小票机

    背景描述: 一家大中型批发及门店销售企业,经销多种冻食品,业务范围覆盖周边众多区域和城市.成立以来,随着业务量的扩大,产品销售分两大渠道:多门店销售和仓库批发,各门店每天都有大量的零散客户和老客户进行 ...

  4. jQuery-认识JQuery,jQuery选择器

    认识JQuery: 1.window.onload与$(document).ready()的区别 window.onload $(document).ready() 执行时机 必须等待网页中的所有内容 ...

  5. ora-14400插入的分区关键字未映射到任何分区---oracle数据库表过期问题

    楼主解决这个问题ora-14400插入的分区关键字未映射到任何分区,其原因是:分区表过期. 通过使用sql直接修改Date类型的字段可以证实,修改成过期以后的时间出现下列提示,修改成过期之前的则可以. ...

  6. Storm编译打包过程中遇到的一些问题及解决方法

    作者: 大圆那些事 | 文章可以转载,请以超链接形式标明文章原始出处和作者信息 网址: http://www.cnblogs.com/panfeng412/archive/2013/11/30/som ...

  7. ACM 变态最大值

    变态最大值 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解 ...

  8. objective-c 通过类名实例化类

    NSString *myClassName = @"MainScene"; Class myClass = NSSClassFromString(myClassName);

  9. Linux分区练习(1)

    1.作业描述: 4个主分区. 具体实现过程: 打开Linux,在终端中输入:fdisk -uc /dev/sda 可以查看到                   :Command (m for hel ...

  10. 十、ios 模态窗口[实例]

    一.模态窗口概念 对话框一般分为两种类型:模态类型( modal )与非模态类型( modeless ).所谓模态对话框,就是指除非采取有效的关闭手段,用户的鼠标焦点或者输入光标将一直停留在其上的对话 ...