The recursive solution is trivial and I omit it here.

Iterative Solution using Stack (O(n) time and O(n) space):

 /**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in vector which contains node values.
*/
public:
vector<int> postorderTraversal(TreeNode *root) {
// write your code here
vector<int> nodes;
TreeNode* node = root;
TreeNode* lastNode = NULL;
stack<TreeNode*> toVisit;
while (node || !toVisit.empty()) {
if (node) {
toVisit.push(node);
node = node -> left;
}
else {
TreeNode* topNode = toVisit.top();
if (topNode -> right && topNode -> right != lastNode)
node = topNode -> right;
else {
nodes.push_back(topNode -> val);
lastNode = topNode;
toVisit.pop();
}
}
}
return nodes;
}
};

Another sophisticated solution using Morris Traversal (O(n) time and O(1) space):

 /**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in vector which contains node values.
*/
public:
vector<int> postorderTraversal(TreeNode *root) {
// write your code here
vector<int> nodes;
TreeNode* dump = new TreeNode();
dump -> left = root;
TreeNode* node = dump;
while (node) {
if (node -> left) {
TreeNode* predecessor = node -> left;
while (predecessor -> right && predecessor -> right != node)
predecessor = predecessor -> right;
if (!(predecessor -> right)) {
predecessor -> right = node;
node = node -> left;
}
else {
reverseAddNodes(node -> left, predecessor, nodes);
predecessor -> right = NULL;
node = node -> right;
}
}
else node = node -> right;
}
return nodes;
}
private:
void reverseNodes(TreeNode* start, TreeNode* end) {
TreeNode* x = start;
TreeNode* y = start -> right;
TreeNode* z;
while (x != end) {
z = y -> right;
y -> right = x;
x = y;
y = z;
}
}
void reverseAddNodes(TreeNode* start, TreeNode* end, vector<int>& nodes) {
reverseNodes(start, end);
TreeNode* node = end;
while (true) {
nodes.push_back(node -> val);
if (node == start) break;
node = node -> right;
}
reverseNodes(end, start);
}
};

[LintCode] 二叉树的后序遍历的更多相关文章

  1. lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历

    题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...

  2. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  3. LintCode-68.二叉树的后序遍历

    二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [3,2,1] 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code / ...

  4. PTA L2-006 树的遍历-二叉树的后序遍历+中序遍历,输出层序遍历 团体程序设计天梯赛-练习集

    L2-006 树的遍历(25 分)   给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤),是二叉树中结点的 ...

  5. LeetCode:二叉树的后序遍历【145】

    LeetCode:二叉树的后序遍历[145] 题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

  6. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  7. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  8. leecode刷题(30)-- 二叉树的后序遍历

    leecode刷题(30)-- 二叉树的后序遍历 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 思路 ...

  9. PYTHON经典算法-二叉树的后序遍历

    二叉树的后序遍历 问题描述 给出一个二叉树,返回其节点值的后序遍历 问题示例 给出一个二叉树{1,x,2,3}其中x表示空.后序遍历为[3,2,1] 这个图怎么画的呢?答案 需要注意的地方是:bina ...

随机推荐

  1. SQL面试题: 数据库中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列 ,当B列大于C列时选择B列否则选择C列 ,

    1.用一条sql语句 select (case when a>b then a else b end ),(case when b>c then b esle c end)  from 表 ...

  2. ParameterizedThreadStart,ThreadStart的使用,线程Thread传参数

    Thread threadWithParam = new Thread(new ParameterizedThreadStart(new ThreadTest().ShowMsg));//thread ...

  3. TCP网络传输, 数据类型的问题

    转载: http://blog.csdn.net/highfly591/article/details/45309239 1.采用TCP传输时, 应用层为什么要做超时重传: tcp保证数据可靠传输,传 ...

  4. unity, GetComponent<MeshRenderer>().sharedMaterial 与 GetComponent<MeshRenderer>().material

    我多个物体用的是同一个material,当我用gameObject.GetComponent<MeshRenderer>().sharedMaterial.SetColor("_ ...

  5. rabbitmq文章源

    网易杭研后台技术中心的博客 rabbitmq topic简单demo http://blog.csdn.net/cugb1004101218/article/details/21243927?utm_ ...

  6. ado连接sql server

    //ado连接sql server //头文件加上以下这句. #import "C:\Windows\system\msado15.dll" no_namespace rename ...

  7. Python学习之read()方法

    read([size [,chars [,firstline]]]) 含义: 从文本流(io.TextIOWrapper)中解码数据并返回字符串对象.

  8. 微信中调起qq

    http://wpa.qq.com/msgrd?uin={$qq}&menu=yes

  9. 手把手教你Chrome扩展开发:本地存储篇

    手把手教你开发chrome扩展一:开发Chrome Extenstion其实很简单 手把手教你开发Chrome扩展二:为html添加行为 手把手教你开发Chrome扩展三:关于本地存储数据 HTML5 ...

  10. JMeter基础之-使用技巧

    在这此对新版本jmeter的学习+温习的过程,发现了一些以前不知道的功能,所以,整理出来与大分享.本文内容如下. 如何使用英文界面的jmeter 如何使用镜像服务器 Jmeter分布式测试 启动Deb ...