LeetCode: Binary Tree Postorder Traversal [145]
【题目】
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?
【题意】
非递归实现兴许遍历
【思路】
维护两个栈,一个栈用来存储标记,标记对应的结点的右子树是否已经被遍历。还有一个栈存储树节点,用以模拟后序遍历。
【代码】
/**
* Definition for binary tree
* 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) {
vector<int>result;
TreeNode*node=root;
stack<TreeNode*>st;
stack<bool>st_flag; while(node){
st.push(node);
st_flag.push(false);
node=node->left;
}
while(!st.empty()){
bool status = st_flag.top();
if(!status){
//訪问右子树
st_flag.pop(); st_flag.push(true);
node = st.top();
node=node->right;
while(node){
st.push(node);
st_flag.push(false);
node=node->left;
}
}
else{
//訪问当前结点
st_flag.pop();
node = st.top(); st.pop();
result.push_back(node->val);
}
}
return result;
}
};
LeetCode: Binary Tree Postorder Traversal [145]的更多相关文章
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Leetcode Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode——Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- LeetCode Binary Tree Postorder Traversal(数据结构)
题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. /** * Definition for a binary tre ...
- leetcode Binary Tree Postorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode Binary Tree Postorder Traversal 二叉树后续遍历
先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...
随机推荐
- 【Visual Studio】error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800” (转)
1.案例一 _MSC_VER 定义编译器的版本.下面是一些编译器版本的_MSC_VER值:MS VC++ 10.0 _MSC_VER = 1600MS VC++ 9.0 _MSC_VER = 1500 ...
- 标准C程序设计七---117
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- 在tomcat发布项目遇到的问题
今天从SVN上把系统导入本地发生了异常,问题如下: java.math.BigInteger cannot be cast to java.lang.Long 百度一番后发现是因为使用Mysql8.0 ...
- hdu 1787(欧拉函数)
GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- VUE之Router命令行警告:Named Route 'Home' has a default child route. 解决办法
Named Route 'Home' has a default child route. When navigating to this named route (:to="{name: ...
- Manajro17配置
:Manjaro近来点击率一直排行第一,比较适合笔记本使用,这次从下载刻录开始讲述: 1: 下载刻录 因为一些环境特殊原因,官网下载还是比较慢的,普通用户还是从清华镜像源下载比较快,当然你也可以选择官 ...
- HDU 5212 Code【莫比乌斯反演】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5212 题意: 给定序列,1≤i,j≤n,求gcd(a[i],a[j])∗(gcd(a[i],a[j] ...
- Codeforces 599E Sandy and Nuts(状压DP)
题目链接 Sandy and Nuts 题意大概就是给出限制条件求出在该限制条件下树的种数. #include <bits/stdc++.h> using namespace std; # ...
- spring boot原理分析
1.分析spring-boot-starter-parent <parent> <groupId>org.springframework.boot</groupId> ...
- C# 将 WebService 封装成动态库
C# 将 WebService 封装成动态库 服务与服务之间的远程调用,经常会通过Web Service来实现,Web Service是支持跨语言调用的,可以是java调用c++或c#调用java等, ...