题目:

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?

解题思路:

后序遍历的非递归实现与前序遍历和中序遍历的非递归不同,对于根节点,必须当其右孩子都访问完毕后才能访问,所以当根节点出栈时,要根据标志位判断是否为第一次出栈,如果是,则将其标志位置为FALSE,然后再次入栈,先访问其右孩子,当某个节点出栈时且其标志位为false,说明该节点为第二次出栈,即表示其右孩子都已处理完毕,可以访问当前节点了

实现代码:

#include <iostream>
#include <vector>
#include <stack>
using namespace std; /**
Given a binary tree, return the postorder traversal of its nodes' values.
*/ struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; void addNode(TreeNode* &root, int val)
{
if(root == NULL)
{
TreeNode *node = new TreeNode(val);
root = node;
}
else if(root->val < val)
{
addNode(root->right, val);
}
else if(root->val > val)
{
addNode(root->left, val);
}
} void printTree(TreeNode *root)
{
if(root)
{
cout<<root->val<<" ";
printTree(root->left);
printTree(root->right);
}
}
struct Node
{
TreeNode *treeNode;
bool isFirst;
}; class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> ivec;
if(root)
{
stack<Node*> istack;
TreeNode *p = root;
while(!istack.empty() || p)
{
while(p)
{
Node *tmpNode = new Node();
tmpNode->treeNode = p;
tmpNode->isFirst = true;
istack.push(tmpNode);
p = p->left;
}
if(!istack.empty())
{
Node *node = istack.top();
istack.pop();
if(node->isFirst)//第一次出栈,
{
node->isFirst = false;
istack.push(node);
p = node->treeNode->right;
}
else//表示其右孩子都已经访问了,可以访问当前节点了
{
ivec.push_back(node->treeNode->val);
}
} } }
return ivec; }
};
int main(void)
{
TreeNode *root = new TreeNode();
addNode(root, );
addNode(root, );
addNode(root, );
addNode(root, );
printTree(root);
cout<<endl; Solution solution;
vector<int> v = solution.postorderTraversal(root);
vector<int>::iterator iter;
for(iter = v.begin(); iter != v.end(); ++iter)
cout<<*iter<<" ";
cout<<endl; return ;
}

LeetCode145:Binary Tree Postorder Traversal的更多相关文章

  1. LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)

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

  2. lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历

    题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...

  3. LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard

    题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...

  4. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

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

  5. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  6. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

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

  7. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  8. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  9. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. android笔记:Service

    服务:在后台运行,没有界面的组件. 服务生命周期如下: 两种启动方式: 1.startService(): onCreate()-->onStartCommand()-->onDestro ...

  2. PAT L2-008 最长对称子串(模拟字符串)

    对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11. 输入格式: 输入在一 ...

  3. [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  4. 7-将本地的javaweb项目部署到Linux服务器的一般操作

    一.基本流程介绍: 1.安装tomcat;2.安装mysql;3.将本地的javaweb导出成.war文件,传到服务器的tomcat/webapps/下面4.将数据库文件导出成.sql文件,传到服务器 ...

  5. vc通过webbrowser操作ie元素

    1>需要引用 webbrowser2.h,mshtml.h //m_web绑定的webbrowser的变量 CComQIPtr<IHTMLDocument2,&IID_IHTMLD ...

  6. Spring框架整合JUnit单元测试

    1. 为了简化了JUnit的测试,使用Spring框架也可以整合测试 2. 具体步骤 * 要求:必须先有JUnit的环境(即已经导入了JUnit4的开发环境)!! * 步骤一:在程序中引入:sprin ...

  7. TPM、read counts、RPKM/FPKM你选对了吗?

    TPM.read counts.RPKM/FPKM你选对了吗? 已有 3940 次阅读 2017-12-15 15:04 |个人分类:RNA-seq|系统分类:科普集锦|关键词:RNA-seq| RN ...

  8. Jmeter中正则表达式不区分大小写进行匹配

    (?i)<r i="([A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12})" ...

  9. $.post 提示错误: Uncaught SyntaxError: Unexpected token :

    $.post("addRecommond",{"productId":productId,"categoryCode":categoryCo ...

  10. 运行 .jar dos 命令

    命令行进入 jar  所在文件夹 执行 java -jar  a.jar;