题意:

  用迭代法输出一棵二叉树的后序遍历结果。

思路:

  (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了。

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ans;
if(root==NULL) return ans; stack<TreeNode*> stac1; stac1.push(root);
stack<int> stac2; stac2.push(-);//表示哪个孩子已经被遍历,-1表示其孩子未被遍历 while(!stac2.empty())
{
TreeNode *top=stac1.top();
int &dir=stac2.top();
if(dir<)
{
if(dir==- && top->left)
{
stac1.push(top->left);
dir=;
stac2.push(-);
}
else if(top->right)
{
stac1.push(top->right);
dir=;
stac2.push(-);
}
else dir=;
}
else
{
ans.push_back(top->val);
stac2.pop();
stac1.pop();
}
}
}
};

AC代码

  (2)用一个栈,模拟逆过程,先输出根,再输出右子树,再输出左子树,最后将输出结果反置即可。这样的好处就是不需要记录根节点了,这和层次遍历的过程差不多。

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> ans;
if(root==NULL) return ans; stack<TreeNode*> stac;
stac.push(root); while(!stac.empty())
{
TreeNode *top=stac.top();
ans.push_back(top->val);
stac.pop();
if(top->left) stac.push(top->left);
if(top->right) stac.push(top->right);
}
reverse(ans.begin(),ans.end());
}
};

AC代码

  

  (3)O(1)的空间,依然O(n)的复杂度。待写。。。。

LeetCode Binary Tree Postorder Traversal(数据结构)的更多相关文章

  1. LeetCode: Binary Tree Postorder Traversal 解题报告

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

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

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

  3. Leetcode Binary Tree Postorder Traversal

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

  4. [Leetcode] Binary tree postorder traversal二叉树后序遍历

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

  5. [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索

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

  6. LeetCode——Binary Tree Postorder Traversal

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

  7. LeetCode: Binary Tree Postorder Traversal [145]

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

  8. leetcode Binary Tree Postorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  9. leetcode Binary Tree Postorder Traversal 二叉树后续遍历

    先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...

随机推荐

  1. vs2010 快捷键大全 (转)

    VS2010版快捷键 Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O ...

  2. 修改weblogic部署的应用名称

    通过weblogic管理后台console进行发布本地项目的时候,它会默认以WEB-INF的上一级目录作为访问路径,如,假如你的项目WEB-INF目录的上一层是WebRoot,那么发布后,访问的路径默 ...

  3. jquery返回上一页面

    window.location.href=document.referrer;   返回然后刷新 window.history.back(-1);  返回不刷新

  4. 网页优化URI(http URI scheme与data URI scheme)

    网页优化的一大首要任务是减少HTTP 请求 (http request) 的次数,例如通过合并多个JS文件,合并CSS样式文件.除此之外,还有一个data URL 的密技,让我们直接把图像的内容崁入网 ...

  5. bzoj 2152: 聪聪可可

    #include<cstdio> #include<algorithm> using namespace std; ; ],head[N],son[N],f[N],d[N],r ...

  6. hdu 4622 Reincarnation

    http://acm.hdu.edu.cn/showproblem.php?pid=4622 用字典树把每一个字符串对应成一个整数 相同的字符串对应到相同的整数上 把所用的串对应的整数放在一个数组里 ...

  7. 学习linux与wp8.1——启航

    现在不知不觉已经大三了,而本专业的东西没有多大感兴趣的,我看好wp开发和linux开发. 为什么要学习wp开发?其实就是一种兴趣,我手中有部620而已,学着学着就感兴趣了,所以打算继续学下去.同时,我 ...

  8. 如何在redhat下安装办公软件(openoffice)

    在redhat的client版本中自带有办公软件libreoffice,而在server版的redhat中却没有自带的办公软件,那么,如何在redhat的server版下安装办公软件呢? 方法一:配置 ...

  9. Section 1.4 The Clocks

    0 0 虽然不知不觉做到了Section 1.4了,但是都没有把做题的想法和代码发到这里… 本来今天想从Section 1.2补起来然后发现之前做的题都忘了…(Name That Number那道题是 ...

  10. PDF创建及动态转换控件程序包ActivePDF Portfolio

    ActivePDF Portfolio是将4个activePDF最优秀的服务器产品捆绑成一个价格适中的控件程序包.它提供了开发一个完整的服务器端的PDF解决方案所需的一切. 具体功能: activeP ...