二叉树的后序遍历

用标记右子树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. caller和callee

    我们先来看下caller. caller:返回一个对函数(该函数调用了当前函数)的引用. functionName.caller:functionName对象是所执行函数的名称. 说明 对于函数来说, ...

  2. Linux网络参数设置

    1.ifconfig  查询.设定网络卡与ip     设置桥接网络 # vi /etc/sysconfig/network-script/ifcfg-br0       DEVICE=br0     ...

  3. bootstrap模版

    http://demo.cssmoban.com/cssthemes3/cpts_274_nz/forms.html

  4. spring学习

    http://blog.csdn.net/chjttony/article/details/6301523 http://blog.csdn.net/chjttony/article/details/ ...

  5. ajax浅析---ScriptManager

    使用ScriptManager控件 它用来处理页面上的所有组件以及页面局部更新,生成相关的客户端代理脚本以便能够在JavaScript中访问Web Service,所有需要支持ASP.NET AJAX ...

  6. 宿主系统为Ubuntu 14,CentOS 6.5 安装VirtualBox增强工具失败:Building the OpenGL support module[FAILED]

    安装先前的笔记:CentOS 6.3 中安装VirtualBOX增强工具失败:Building the main Guest Additions module[FAILED],执行了以下命令 #安装 ...

  7. Ubuntu 下apache2开启rewrite隐藏index.php

    为了实现 http://www.example.com/route/route 而不是 http://www.example.com/index.php/route/route 需要开启apache2 ...

  8. .apache2 设置多个虚拟域名

    <VirtualHost 127.0.0.2:80> ServerName www.xylilun.cn DocumentRoot E:/www/ylll <Directory E: ...

  9. 五款最佳Linux下载管理器推荐

    导读 新的Linux用户从Windows转换过来时面临的困难之一就是,找到一款优秀的下载管理器.如果你是或曾经是Windows用户,可能熟悉互联网下载管理器(IDM).下载加速器Plus(DAP)之类 ...

  10. spring mvc 页面编码和数据库编码 中文出现乱码

    1.前台与后台交互的时候,后台获取的中文为乱码,而且插入数据库数据也为乱码. 修改web.xml 添加编码的过滤器,全部设置为utf-8(注意加上forceEncoding) <filter&g ...