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. HDU 4858 项目管理 分块

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4858 题解: 下面说一个插入查询时间复杂度为sqrt(m)的算法: 对每个点定义两个值:val,su ...

  2. 《DSP using MATLAB》示例Example4.13

    代码: b = [1, 0, -1]; a = [1, 0, -0.81]; % [R, p, C] = residuez(b,a); Mp = (abs(p))' Ap = (angle(p))'/ ...

  3. no-jquery 02 DOM

    DOM Manipulation Creating Elements // IE 5.5+ document.createElement('div'); Inserting Elements Befo ...

  4. 分享Kali Linux 2016.2第41周镜像虚拟机

    分享Kali Linux 2016.2第41周镜像虚拟机该虚拟机使用Kali Linux 2016.2第41周镜像文件安装而成,系统已经更新到10月12日.里面已经进行如下配置:(1)设置官方软件源( ...

  5. CSS3设置多张背景图片

    background-image:url("1.jpg"),url("2.jpg"),url("3.jpg");background-rep ...

  6. 静态数据成员(面向对象的static关键字)

    静态数据成员: 在类内数据成员的声明前加上关键字static,该数据成员就是类内的静态数据成员.先举一个静态数据成员的例子. #include<iostream> using namesp ...

  7. 使用Gson排除特定字段

    http://blog.csdn.net/hknock/article/details/51037564

  8. The 2015 China Collegiate Programming Contest C. The Battle of Chibi hdu 5542

    The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Othe ...

  9. An Unfair Game-[ACdream1035]

    Problem Description There are n people and n target, everyone should get one target, no two people g ...

  10. BZOJ3346 : Ural1811 Dual Sim Phone

    首先将边进行去重,那么有$n\geq\sqrt{m}$. 然后二分答案,转化为判定是否存在两个点它们的出边集合的并集为全集. 那么这两个点必然满足$deg_x+deg_y\geq n$. 不妨设$de ...