LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译
给定一个二叉树。返回其兴许遍历的节点的值。
比如:
给定二叉树为 {1。 #, 2, 3}
1
\
2
/
3
返回 [3, 2, 1]
备注:用递归是微不足道的,你能够用迭代来完毕它吗?
原文
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?
分析
直接上代码……
vector<int> postorderTraversal(TreeNode* root) {
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
v.push_back(root->val);
}
return v;
}
/**
* 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> v;
void postorderTraversalIter(TreeNode *root, stack<TreeNode*> &stac) {
if (root == NULL) return;
bool hasLeft = root->left != NULL;
bool hasRight = root->right != NULL;
stac.push(root);
if (hasRight)
stac.push(root->right);
if (hasLeft)
stac.push(root->left);
if (!hasLeft && !hasRight)
v.push_back(root->val);
if (hasLeft) {
root = stac.top();
stac.pop();
postorderTraversalIter(root, stac);
}
if (hasRight) {
root = stac.top();
stac.pop();
postorderTraversalIter(root, stac);
}
if (hasLeft || hasRight)
v.push_back(stac.top()->val);
stac.pop();
}
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> stac;
postorderTraversalIter(root, stac);
return v;
}
};
另有两道相似的题目:
LeetCode 94 Binary Tree Inorder Traversal(二叉树的中序遍历)+(二叉树、迭代)
LeetCode 144 Binary Tree Preorder Traversal(二叉树的前序遍历)+(二叉树、迭代)
LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)的更多相关文章
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- Java for LeetCode 145 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- leetcode 145. Binary Tree Postorder Traversal ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- Leetcode#145 Binary Tree Postorder Traversal
原题地址 递归写法谁都会,看看非递归写法. 对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子.所以,最直 ...
随机推荐
- 【Docker】mesos如何修改hostport默认端口范围?
1.marathon文档:https://mesosphere.github.io/marathon/docs/native-docker.html Static port mapping: It's ...
- g++ 静态库连接顺序的巨坑
在编译最新版本(12.04)的alljoyn的chat示例的时候,想使用bundle daemon,依照在以前的经验修改文件:alljoyn-14.02.00-src/build/linux/x86_ ...
- install.Android
It was not possible to complete an automatic installation. This might be due to a problem with your ...
- 转:java中数组与List相互转换的方法
1.List转换成为数组.(这里的List是实体是ArrayList) 调用ArrayList的toArray方法. toArray public <T> T[] toArray(T[] ...
- MPlayer源代码分析
http://blog.csdn.net/leixiaohua1020/article/details/11885509 一.Mplayer支持的格式 MPlayer是一个LINUX下的视频播放器,它 ...
- JAVA 画图机制
java学习脚印:深入java绘图机制 写在前面 封装性越好的类在使用时,只要清楚接口即可,而不应该让程序员了解其内部结构; 对于平常的绘图来讲,java绘图机制无需了解太多,但是朦胧容易产生错误,绘 ...
- jshint错误
这条命令即可. npm install --save-dev jshint gulp-jshint
- 算法笔记_107:蓝桥杯练习 算法提高 学霸的迷宫(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗.但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要 ...
- 数学之路-分布式计算-linux/unix技术基础(4)
pwd显示当前文件夹,ls查看文件夹下的文件,cd 进入文件夹 -bash-4.2$ pwd /home/myhaspl-bash-4.2$ lsabc hadoop-2.4.1 ...
- java new map
import com.google.common.collect.Maps; public static Map<String, Object> configMap2 = new Conc ...