题目:

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. VSTO学习笔记(三) 开发Office 2010 64位COM加载项

    原文:VSTO学习笔记(三) 开发Office 2010 64位COM加载项 一.加载项简介 Office提供了多种用于扩展Office应用程序功能的模式,常见的有: 1.Office 自动化程序(A ...

  2. 使用EXCEL设置“下拉菜单”选项功能

    原创作品.出自 "深蓝的blog" 博客.欢迎转载.转载时请务必注明出处,否则有权追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...

  3. 鸟哥Linux私房菜知识点总结6到7章

    近期翻看了一本<鸟哥的Linux私房菜>.这是一本基础的书,万丈高楼平地起.会的不多但能够学.这是我整理的一些知识点.尽管非常基础.希望和大家共同交流. 第6章主机规划与磁盘分区 1.在进 ...

  4. oracle动态注冊參数local_listener

    local_listener參数有两种书写格式,提供了不同的功能. 监听文件上,1521和1526port上都有动态监听port. [oracle@dbsv admin]$ cat listener. ...

  5. LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  6. Python数据结构-序表

    序表解包: list=['aa','bb','cc'] [a1,a2,a3]=list

  7. 6 款好用的 PC+Android 同步 GTD 软件

    6 款好用的 PC+Android 同步 GTD 软件 最近老鼠工作积极性比较高(其实只要是买平板电脑的欲望在鼓舞着干劲),所以每天很多任务安排,为了不混乱,免不了要用 GTD(Go to do)软件 ...

  8. hdu 折线切割平面 (java)

    问题: 仅仅要找到规律问题就攻克了,在做题时应该细致去发现数与数之间的联系. 折线切割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  9. RabbitMq消息序列化简述

    涉及网络传输的应用.序列化不可避免. 发送端以某种规则将消息转成byte数组进行发送. 接收端则以约定的规则进行byte[]数组的解析. 序列化的选择能够是jdk序列化,hessian,jackson ...

  10. Windows下文件或文件夹不能删除时的解决办法

    windows在删除文件或文件夹时,提示文件或文件夹被占用而无法删除 解决办法:win7: winxp:需要借助第三方工具Unlocker.360.Process Explorer(这个是微软支持的) ...