二叉树的后序遍历

用标记右子树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) ☆的更多相关文章

  1. 【leetcode】Binary Tree Postorder Traversal

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...

  2. 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)

    这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

  3. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  4. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

  5. 【Leetcode】【hard】Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  6. 【leetcode】Binary Tree Preorder Traversal (middle)★

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  7. 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)

    这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...

  8. 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)

    这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...

  9. 【Leetcode】Binary Tree Traversal

    把三个二叉树遍历的题放在一起了. 递归写法太简单,就不再实现了,每题实现了两种非递归算法. 一种是利用栈,时间和空间复杂度都是O(n). 另一种是借助线索二叉树,也叫Morris遍历,充分利用树中节点 ...

随机推荐

  1. "当前方法的代码已经过优化,无法计算表达式的值"的这个错误的解决方案!!!

    http://blog.useasp.net/archive/2012/09/12/how-to-debug-dotnet-framework-source-when-throw-the-code-o ...

  2. Error: [ng:areq] Argument 'xxxx' is not a function, got undefined

    "Error: [ng:areq] Argument 'keywords' is not a function, got undefined" 代码类似这样的: <div n ...

  3. VTK初学一,c_Line_CellArray线段的CellArray绘制

    VTK窗口默认坐标方向: #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE ...

  4. Codeforces Round #270 1002

    Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...

  5. 【PHP面向对象(OOP)编程入门教程】22.把对象串行化serialize()方法,__sleep()方法,__wakeup()方法

    有时候需要把一个对象在网络上传输,为了方便传输,可以把整个对象转化为二进制串,等到达另一端时,再还原为原来的对象,这个过程称之为串行化(也叫序列化), 就像我们现在想把一辆汽车通过轮船运到美国去,因为 ...

  6. CentOS启用sudo,禁用root远程登录

    CentOS默认不启用sudo,且可以直接用超级管理员身份登录服务器.ubuntu这方面做得比较好,为了安全,减小误操作带来的损失,还是推荐启用sudo. 1.添加sudo用户 执行 visudo 命 ...

  7. ExtJs xtype类型介绍

    自定义组件在定义的时候可以通过xtype配置为组件指定xtype短名称,此后创建对象可以通过xtype来创建自定义对象了,示例代码如下: Ext.define('MyApp.PressMeButton ...

  8. Ucenter,Discuz

    http://www.zb7.com/discuz/   (详细资料网站) Discuz主要是配置前台的模板制作,在二次开发时. UCenter主要是客户端的数据库的链接.client/.

  9. mysql远程登录权限修改ubuntu

    mysql默认只允许在localhost主机登录,如果想要通过远程登录管理,需要修改相应的权限. 方法一 首先:开启mysql所在主机的3306端口,或者关闭防火墙. service iptables ...

  10. hdu1536&&hdu3023 SG函数模板及其运用

    S-Nim Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status ...