[LeetCode145]Binary Tree Postorder Traversal
题目:
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的更多相关文章
- LeetCode145 Binary Tree Postorder Traversal Java题解(递归 迭代)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- 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 ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
随机推荐
- Boost中timer的简易用法
boost::asio::deadline_timer timer_; timer_(io_service), timer_.expires_from_now(boost::posix_time::s ...
- Wix打包系列(二)用户界面和本地化操作
原文:Wix打包系列(二)用户界面和本地化操作 上一章节,我们已经大概知道如何对文件进行打包安装,不过我们也注意到,通过对Sample.wxs的编译链接,生成的msi安装包没有任何用户界面,只有一个安 ...
- uboot启动阶段修改启动参数方法及分析
作者:围补 本来启动方式这节不是什么复杂的事儿,不过想简单的说清楚明白,还真是不知道怎么组织.毕竟文字跟有声语言表达有别.但愿简单的东西别让我讲的太复杂! Arm板系统文件一般有三个——bootloa ...
- C#基础总结之Attribute
Attribute是什么 Attribute的中文姓名 为什么我要拿一段文字来说Attribute的中文姓名呢?答案是:因为这很重要.正所谓“名”不正,则言不顺:另外重构手法中有一种很重要的方法叫重命 ...
- 安装 MYSQL exec: g++: not found 报错
解决办法: yum install -y gcc-c++
- Effective C++:条款38:通过一个复杂的模具has-a要么“基于一些实现”
(一) public继承是"is-a"关联,"has-a"或"依据某物实现出(is-implemented-in-terms-of)"的意思 ...
- warning: directory not found for option ' '
解决: 选择项目名称-->Targets-->Build Settings-->Search Paths-->Library Search Paths 删除相应路径
- Beginning Python From Novice to Professional (4) - 演示样本格式字符串
$ gedit price.py #!/usr/bin/env python width = input('Please enter width: ') price_width = 10 item_w ...
- DirectX11 学习笔记9 - 动态顶点缓冲区 和 静态顶点缓冲区
首先,什么是缓冲区: 缓冲区是.fx文件的影响(.ps .vs还) 一种数据结构,其定义了.为.fx和cpp数据通信文件. 例: //--------------------------------- ...
- ECshop lib_base.php on line 1241 错误解决方法
ECSHOP做的一个网站,突然报这个错误,整个网站打不开,后来找了很久,终于找到这个方法,亲测可用 Notice: Undefinedvariable: data in D:\wwwroot\KISS ...