【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遍历,充分利用树中节点 ...
随机推荐
- mobile touch事件
touch.js 众所周知,mobile与pc 前端开发的不同中,有一点就是事件的不同,mobile上有touchstart,touchmove,touchend等,而pc上用最多的应该还是我们的cl ...
- TTFB-首字节时间简介
百度站长工具里看到有一个"首字节时间"的建议,第一次听说,还真不知道是什么东东.百度站长工具里面的解释是:"浏览器开始收到服务器响应数据的时间=后台处理时间+重定向时间, ...
- 设置二级域名共享一级域名Cookie和删除共享Cookie
设置共享Cookie: 二级域名要想共享一级域名的cookie,只需要设置cookie.Domain = ".一级域名.com"; 删除共享Cookie: HttpCook ...
- POJ 1019 Number Sequence
找规律,先找属于第几个循环,再找属于第几个数的第几位...... Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Ubuntu编译PHP7问题
安装编译依赖 sudo apt-get -y install build-essential git autoconf sudo apt-get build-dep php5 sudo apt-get ...
- ubuntu 15.04 手动安装nginx 1.9.0
平时工作也用nginx,不过用的时候都是已经配好的,只要简单改改参数就可以了.今天在自己的电脑上安装的时候发现没有想象的那么顺利. 纸上得来终觉浅,绝知此事要躬行. 正题: 1.到nginx下载页面获 ...
- 关于JavaScript中的创建对象的学习总结
一.最简单的对象创建方法 在JavaScript中,直接使用Object构造函数或对象字面量都可以很轻易地创建单个对象,缺点是:创建具有同一个接口(标准的OO中的接口概念)的多个对象时,会有大量重复代 ...
- .net 默认时间格式不正确
经测试发现,有时通过在“日期.时间.语言和区域设置”里面设置是无效的. 对应键值为HKEY_LOCAL_MACHINE"SOFTWARE"Microsoft"OLEAUT ...
- 解决ubuntu每次重启屏幕亮度都重置为最高亮度问题
很多朋友都会碰到这个问题,Ubuntu系统,每次通过系统设置修改了屏幕亮度,重启系统都会将屏幕亮度调成最大值,很是苦恼. 上网搜索一番发现,修改屏幕亮度的文件是:/sys/class/backligh ...