题意:

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

思路:

  (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. JDE隐藏Constant等(Hide Object)

    Grid中隐藏列,列值可以使用属性配置,但是列表头Constant需要用函数修改,如下所示:

  2. idea tomcat +eclipse式的部署

    使用习惯了eclipse, 还没开始使用maven, 使用idea 有些不太习惯,现在记录下来,以备忘. /*这一步在tomcat使用external source时,其实是不起作用的**/   a. ...

  3. HTML去掉网页IE滚动条

    做了一个页面,与桌面分辨率一样大小,但是在IE全屏(F11)下却显示有滚动条,此教程由软件自学网首发,而火狐确没有.怎么样去掉IE滚动条呢?其实有一个属性就可以解决. 方法1:直接在body里面加上属 ...

  4. MongoDb系列

    这个系列主要总结学习MongoDb过程中的一些经验. 简单介绍及环境搭建 常用命令 C#驱动及应用 管理工具MongoVUE使用

  5. 高效前端优化工具--Fiddler入门教程

    简介: Fiddler是用C#编写的一个免费的HTTP/HTTPS网络调试器.Fiddler是以代理服务器的方式,监听系统的网络数据流动英语中Fiddler是小提琴的意思,Fiddler Web De ...

  6. hdu 4607 Park Visit

    http://acm.hdu.edu.cn/showproblem.php?pid=4607 先求树的直径 方法:两遍bfs ,任选一点 a  求到a点最远的一点b ,然后 求到b点最远点 c 这样 ...

  7. WDCP管理面板安装启动EXIF、bcmath完整步骤

    一般我们网站建设的需要,如果使用WDCP面板默认的功能就足够使用,如果需要特殊程序的特定组件支持,就需要独立的安装支持组件.比如一位朋友的程序需要支持EXIF.bcmath组件,这不老蒋寻找解决方法, ...

  8. async = require('async')

    var mongoose = require('mongoose'), async = require('async'); mongoose.connect('localhost', 'learn-m ...

  9. [转载]Android核心分析

    2013-12-19 15:44:03 转载自: http://blog.csdn.net/column/details/androidcore.html 很好的文章,阅读请跳转到转载链接,转载备以后 ...

  10. Problem C 链表

    Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报 ...