【leetcode】Binary Tree Postorder Traversal (hard) ☆
二叉树的后序遍历
用标记右子树vector的方法
vector<int> postorderTraversal(TreeNode *root) {
vector<int> ans;
vector<TreeNode *> stack;
vector<bool> isRight;
stack.push_back(root);
isRight.push_back(false);
TreeNode * pNode = NULL;
while(!stack.empty())
{
while((pNode = stack.back()) != NULL)
{
pNode = pNode->left;
stack.push_back(pNode);
isRight.push_back(false);
}
while(isRight.back() == true)
{
stack.pop_back();
isRight.pop_back();
ans.push_back(stack.back()->val);
}
stack.pop_back();
isRight.pop_back();
if(!stack.empty())
{
pNode = stack.back();
stack.push_back(pNode->right);
isRight.push_back(true);
}
}
return ans;
}
仅用一个栈的方法https://oj.leetcode.com/discuss/14118/post-order-traversal-using-two-satcks
vector<int> postorderTraversal(TreeNode *root) {
stack<TreeNode *> st;
vector<int> vRet;
TreeNode *p, *pre = root;
if (!root) return vRet;
p = root->left;
st.push(root);
while (!st.empty() )
{
while (p) { st.push(p); p = p->left; }
p = st.top();
while (!p->right || pre==p->right)
{
vRet.push_back(p->val);
pre = p;
st.pop();
if (st.empty() ) break;
p = st.top();
}
p = p->right;
}
return vRet;
}
【leetcode】Binary Tree Postorder Traversal (hard) ☆的更多相关文章
- 【leetcode】Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...
- 【Leetcode】【hard】Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【leetcode】Binary Tree Preorder Traversal (middle)★
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)
这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...
- 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)
这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...
- 【Leetcode】Binary Tree Traversal
把三个二叉树遍历的题放在一起了. 递归写法太简单,就不再实现了,每题实现了两种非递归算法. 一种是利用栈,时间和空间复杂度都是O(n). 另一种是借助线索二叉树,也叫Morris遍历,充分利用树中节点 ...
随机推荐
- vector族函数
本文原创,转载请注明出处,本人Q1273314690 vector(mode = "logical", length = 0) as.vector(x, mode = " ...
- 网站SEO优化之添加Sitemap文件。
Sitemap.xml 故名思意就是站点地图文件,可以指引Google spider 收录相应网页.正确地使用Google Sitemap,可以确保让Google spider 不遗漏网站内的任何页面 ...
- golang的json操作
package main import ( "encoding/json" "fmt" "os" ) type ConfigStruct s ...
- HttpClient连接池的连接保持、超时和失效机制
HTTP是一种无连接的事务协议,底层使用的还是TCP,连接池复用的就是TCP连接,目的就是在一个TCP连接上进行多次的HTTP请求从而提高性能.每次HTTP请求结束的时候,HttpClient会判断连 ...
- jquery左右滑动效果的实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- nodejs,node原生服务器搭建实例
nodejs,node原生服务器搭建实例
- 通过Unity3d创建二维码(利用zxing2.2)
http://blog.csdn.net/liulala16/article/details/14521979 2013-11-08 14:53 1965人阅读 评论(3) 收藏 举报 首先 下载ZX ...
- 流畅web动画的十个法则
from me: web动画能够带来一个非常酷炫的效果,能够让页面有一个更好的用户体验.对于良好的动画性能没有高招,除了将大量的时间放在测试和优化,当然最重要的还是要易于维护. 流畅web动画的十大法 ...
- OpenCV图像轮廓检测
轮廓检测: 轮廓检测的原理通俗的说就是掏空内部点,比如原图中有3*3的矩形点.那么就可以将中间的那一点去掉. 一.关键函数1.1 cvFindContours函数功能:对图像进行轮廓检测,这个函数将 ...
- PHP中九大缓存技术总结
PHP缓存包括PHP编译缓存和PHP数据缓存两种.PHP是一种解释型语言,属于边编译边运行的那种.这种运行模式的优点是程序修改很方便,但是运行效率却很低下.PHP编译缓存针对这种情况做改进处理,使得P ...