题意:

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

思路:

  (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. [转]网络时间的那些事及 ntpq 详解

    Gentoo(也许其他发行版也是?)中 "ntpq -p" 的 man page 只有简短的描述:“打印出该服务器已知的节点列表和它们的状态概要信息.” 我还没见到关于这个命令的说 ...

  2. 经典DP 二维换一维

    HDU 1024  Max Sum Plus Plus // dp[i][j] = max(dp[i][j-1], dp[i-1][t]) + num[j] // pre[j-1] 存放dp[i-1] ...

  3. ajax使用jquery的实现方式

    1.jquery的ajax方法. $("#ajaxbtn").click(function(){ $.ajax({ url:"json.do", beforeS ...

  4. HDU 4405 Aeroplane chess 概率DP 难度:0

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 明显,有飞机的时候不需要考虑骰子,一定是乘飞机更优 设E[i]为分数为i时还需要走的步数期望,j为某个可能 ...

  5. Delphi日期时间 UNIX

    Delphi日期时间,就是常见的 2014-05-02 10:37:35 --------------------------------------------------------------- ...

  6. UITableViewCell Property “icon” cannot be found in forward class object “DJWeiBo”

    UITableViewCell 自定义表格 实体属性不显示错误 Property “icon” cannot be found in forward class object “DJWeiBo”引入实 ...

  7. Cisco IOS basic system management command reference

    absolute : to specify an absolute time for a time-range (in time-range configuration mode) no absolu ...

  8. shell变量的使用

    转载请标明http://www.cnblogs.com/winifred-tang94/ shell环境中变量有三种类型: a.  环境变量:可以在shell脚本中直接利用“$环境变量名称”的形式引用 ...

  9. 如何使用SVN管理我们的源代码

    今天把公司的SVN服务器配置给做了一下,根据公司部门的不同,划分了不同的访问目录,并给不同目录配置了相应的权限,算是把这份差事给干完了,但其实我对自己的工作是不满意和有遗憾的,因为目前公司的SVN服务 ...

  10. php获取远程文件大小

    获取本地文件大小filesize()就可以了,但是如何获取远程文件的大小呢? 这里介绍三个方法来获取远程文件的大小. 方法1:get_headers <?php get_headers($url ...