【题目】

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?

【题意】

非递归实现兴许遍历

【思路】

维护两个栈,一个栈用来存储标记,标记对应的结点的右子树是否已经被遍历。还有一个栈存储树节点,用以模拟后序遍历。

【代码】

/**
* Definition for binary tree
* 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>result;
TreeNode*node=root;
stack<TreeNode*>st;
stack<bool>st_flag; while(node){
st.push(node);
st_flag.push(false);
node=node->left;
}
while(!st.empty()){
bool status = st_flag.top();
if(!status){
//訪问右子树
st_flag.pop(); st_flag.push(true);
node = st.top();
node=node->right;
while(node){
st.push(node);
st_flag.push(false);
node=node->left;
}
}
else{
//訪问当前结点
st_flag.pop();
node = st.top(); st.pop();
result.push_back(node->val);
}
}
return result;
}
};

LeetCode: Binary Tree Postorder Traversal [145]的更多相关文章

  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(数据结构)

    题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. /** * Definition for a binary tre ...

  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. Bootstrap 模态框(Modal)插件数据传值

    原文:http://blog.csdn.net/baalhuo/article/details/51178154 模态框具体代码如下: <!-- 模态框(Modal) --> <di ...

  2. 莫比乌斯函数之和(51nod 1244)

    莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含平方因子,那么miu(n) = 0.例如 ...

  3. Notepad++中常用的插件【转】

    转自:http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htmls/npp_common_plugins.html 1.4. N ...

  4. Vue.js实战:初识Vue.js

    一.Vue.js是什么 简单小巧的核心,渐进式技术栈,足以应付任何规模的应用. 简单小巧指的是Vue.js 压缩后大小仅有17KB 所谓渐进式(Progressive)就是你一步一步,有阶段性地来使用 ...

  5. es6 Number.isFinite()、Number.isNaN()、Number.isInteger()、Math.trunc()、Math.sign()、Math.cbrt()、Math.fround()、Math.hypot()、Math 对数方法

    ES6在Number对象上,新提供了Number.isFinite()和Number.isNaN()两个方法,用来检查Infinite和NaN这两个特殊值. Number.isFinite()用来检查 ...

  6. MySQL通用编程

    第一阶段:基础入门 第一章:关系模型 第二章:基本查询 第三章:复杂查询 第四章:权限控制 第五章:查询优化 第二阶段:模型设计 第六章:设计选择 第七章:函数依赖 第八章:分解算法 第九章:设计过程 ...

  7. F#周报2019年第21期

    新闻 F#在GitHub上的开发仓库现在变为dotnet/fsharp Ionide 4.0路线图 Fable的五月公告 Visual Studio 2019版本16.1 WinUI 3.0路线图 欢 ...

  8. 详解Java中的字符串

    字符串常量池详解 在深入学习字符串类之前, 我们先搞懂JVM是怎样处理新生字符串的. 当你知道字符串的初始化细节后, 再去写String s = "hello"或String s ...

  9. Java中PO、BO、VO、DTO、POJO、DAO概念及其作用和项目实例图(转)

    PO(bean.entity等命名): Persistant Object持久对象,数据库表中的记录在java对象中的显示状态 最形象的理解就是一个PO就是数据库中的一条记录. 好处是可以把一条记录作 ...

  10. iOS UI08_TableView界面传值

    实现两个界面之间内容的传递 // // MainViewController.m // UI08_TableView界面传值 // // Created by dllo on 15/8/7. // C ...