题目:

Given a list, rotate the list to the right by k places, where k is non-negative.

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

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

For example:
Given binary tree {1,#,2,3},

   1
\
2
/
3

return [3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?

分类:Tree Stack

代码:二叉树非递归后序遍历

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> s;
vector<int> nodes;
TreeNode* cur = root;//u当前访问的结点
TreeNode* lastNode = NULL;//上次访问的结点
while(cur || !s.empty())
{
//一直向左走直到为空为止
while(cur)
{
s.push(cur);
cur = cur->left;
}
cur = s.top();
//如果结点右子树为空或已经访问过,访问当前结点
if(cur->right == NULL || cur->right == lastNode)
{
nodes.push_back(cur->val);
lastNode = cur;
s.pop();
cur = NULL;
}
else
cur = cur->right;
}
return nodes;
}
};

[LeetCode145]Binary Tree Postorder Traversal的更多相关文章

  1. LeetCode145 Binary Tree Postorder Traversal Java题解(递归 迭代)

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

  2. Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历

    给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...

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

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

  4. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  5. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  6. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  7. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  8. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  9. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. poj3678(two-sat)

    传送门:Katu Puzzl 题意:n个点,点的取值可以是0或者1.m条边,有权值,有运算方式(并,或,异或),要求和这条边相连的两个点经过边上的运算后的结果是边的权值.问你有没有可能把每个点赋值满足 ...

  2. HDU1789Doing Homework again(贪婪)

    HDU1789Doing Homework again(贪心) 题目链接 题目大意:给你n们作业的最后期限和过了这个期限没做须要扣的分数.问如何安排能够使得扣分最少. 解题思路:贪心,将扣分多的作业排 ...

  3. 读取生产环境go语言的最佳实践展示

    近期看了一篇关于go产品开发最佳实践的文章,go-in-procution.作者总结了他们在用go开发过程中的非常多实际经验,我们非常多事实上也用到了.鉴于此,这里就简单的写写读后感,兴许我也争取能将 ...

  4. PHP监測memcache服务端的执行状况

    . 代码例如以下,代码为memcache官方代码,引用在此.做一下简单的说明: 1.设置username和password define('ADMIN_USERNAME','admin'); // A ...

  5. 单服务器防护linux iptables脚本

    #!/bin/bashiptables -Fiptables -P INPUT DROPiptables -P OUTPUT ACCEPTiptables -P FORWARD DROP/sbin/i ...

  6. HttpApplication处理对象与HttpModule处理模块

    HttpApplication处理对象与HttpModule处理模块 (第三篇) 一.HttpApplication对象简述 在HttpRuntime创建了HttpContext对象之后,HttpRu ...

  7. MYSQL查询一周内的数据(最近7天的)、最近一个月、最近三个月数据

    如果你要严格要求是某一年的,那可以这样 查询一天: select * from table where to_days(column_time) = to_days(now()); select * ...

  8. H264相关随笔

    DR(Instantaneous Decoding Refresh)--即时解码刷新. I和IDR帧都是使用帧内预测的.它们都是同一个东西而已,在编码和解码中为了方便,要首个I帧和其他I帧区别开,所以 ...

  9. Ice-2.1.2在RHEL Server 5.5上的安装

         因为项目的需要,服务器上的程序需要使用Ice接口与其它程序通信,对方提供了一个Windows版的工程,我要把它移植到Linux服务器上,既然Ice是跨平台跨语言的中间件,想来移植不是很困难, ...

  10. 期望dp专题

    一直不明白为什么概率是正推,期望是逆推. 现在题目做多了,慢慢好像有点明白了 poj2096 收集bug,  有n个种类的bug,和s个子系统.  每找到一个bug需要一天. 要我我们求找到n个种类的 ...