Morris post order traversal algorithm

Sept. 5, 2015

花时间把代码读明白, 比光看书强. 动手写代码, 改代码,  
兴趣是最好的老师. 多记几个例子, 增加情趣.

举个例子关于中序遍历,

4

/     \

2       6

/  \     / \

1   3   5   7

easy way to travel is to remember the order of its position in the horizontal way

现在, 要做Morris 的后序遍历, 有一个技巧, 是在我写完这个C#代码才发现的,


                  9

/    \


               5      8

              / \       \

             1   4     7

                  / \   /

                2   3 6

就是从最左边开始, 遍历五次,

1,

2,

3, 4, 5

6,

7, 8, 9,

最后结果是 1 2 3 4 5 6 7 8 9

加一个dummy node, with left child is the root node.  

Morris post order traversal

blogs to read:
http://www.cnblogs.com/AnnieKim/archive/2013/06/15/MorrisTraversal.html

C# implementation:
https://github.com/jianminchen/MorrisOrder/blob/master/MorrisPostOrder.cs

Morris order in order traversal

https://github.com/jianminchen/MorrisInOrderTraverse/blob/master/Program.cs

时间: 2024-12-19 10:02:15

Morris post order traversal algorithm的相关文章

[LintCode] Binary Tree Leaves Order Traversal

Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty. Have you met this question in a real interview? Yes Example Given binary tree: 1 / 2 3 / \ 4 5 Returns [[4, 5, 3], [

leetcode No103. Binary Tree Zigzag Level Order Traversal

Question: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15

37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,

leetcode No107. Binary Tree Level Order Traversal II

Question: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return its bottom-up l

leetcode No102. Binary Tree Level Order Traversal

Question: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20]

Java [Leetcode 107]Binary Tree Level Order Traversal II

题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

[LeetCode]Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 re

Binary Tree Zigzag Level Order Traversal

原题: 题目解析:这个问题的实质是要我们按成访问二叉树的结点,并返回每层访问的结果,这里要求走Z字,其实就是一行正向一行反向. /* the kernel idea is visit a binary search tree in level and the additional work we have to label the end of one level. */ vector<vector<int> > zigzagLevelOrder(TreeNode *root) {

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example