题目描述

求给定的二叉树的后序遍历。
例如:
给定的二叉树为{1,#,2,3},
   1↵    ↵     2↵    /↵   3↵
返回[3,2,1].
备注;用递归来解这道题太没有新意了,可以给出迭代的解法么?
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?


示例1

输入

复制

{1,#,2,3}

输出

复制

[3,2,1]

/**
 * struct TreeNode {
 *    int val;
 *    struct TreeNode *left;
 *    struct TreeNode *right;
 * };
 */

class Solution {
public:
    /**
     *
     * @param root TreeNode类
     * @return int整型vector
     */
    vector<int> postorderTraversal(TreeNode* root) {
        // write code here
        vector <int> res;
        if (!root)
            return res;
        stack<TreeNode *> st;
        st.push(root);
        while (st.size())
        {
            TreeNode *temp=st.top();
            st.pop();
            res.push_back(temp->val);
            if (temp->left)
                st.push(temp->left);
            if (temp->right)
                st.push(temp->right);
        }
        reverse (res.begin(),res.end());
        return res;
    }
};

leetcode6:binary-tree-postorder-traversal的更多相关文章

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

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

  2. 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 ...

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

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

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

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

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

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

  6. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  7. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

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

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

  9. 145. Binary Tree Postorder Traversal

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

  10. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

随机推荐

  1. C++派生类与基类的关系

    派生类与基类有这些关系: 1.公有派生类从基类继承所有成员和成员函数 2.公有派生类无法直接访问从基类继承的私有成员,但可以通过继承的公共接口访问. 3.公有派生类无法继承基类的友元函数. 4.基类先 ...

  2. PropertySheet外壳扩展AppWizard

    下载source files - 39 Kb 下载Wizard - 17 Kb 本文旨在简化属性表外壳扩展的实现.它紧接我的第一篇文章 处理上下文菜单壳扩展和灵感 由Michael Dunn最优秀的系 ...

  3. Redis使用RDB持久化和AOF持久化的区别 - 小白之所见

  4. nginx完美支持thinkphp3.2.2(需配置URL_MODE=>3 rewrite兼容模式)

    来源:http://www.thinkphp.cn/topic/26637.html 环境:nginx 1.6,thinkphp3.2.2 第一步,修改server块 server { listen ...

  5. day32 Pyhton 模块02复习 序列化

    一. 什么是序列化 在我们存储数据或者网络传输数据的时候. 需要对我们的对象进行处理. 把对象处理成方便存储和传输的数据格式. 这个过程叫序列化 不同的序列化, 结果也不同. 但是目的是一样的. 都是 ...

  6. nginx优化:使用expires在浏览器端缓存静态文件

    一,nginx中expires指令的作用 网站的图片等静态文件一旦发布,通常很少改动, 为了减小对服务器请求的压力,提高用户浏览速度, 我们可以设置nginx中的expires, 使用户访问一次后,将 ...

  7. 为C量身定制的Matrix库

    Matrix库的诞生让矩阵操作在C中也可以玩的很溜! 项目地址:https://github.com/SJ2050SJ/Matrix 文章目录 Matrix的设计框架 Matrix的上手简历 Matr ...

  8. laytable 前台删除后加载当前页数据。

    laytable一次性获取数据后,点击删除调用后台删除.返回成功状态后刷新当前页数据. 渲染方法: //渲染表格 function renderingTable(pageIndex, sourceDa ...

  9. 理解Margin边距塌陷与box-sizing的问题

    父与子塌陷问题 子盒子与父盒子相互影响,margin值会重叠,谁大听谁的 运行结果: box-sizing box-sizing 原始属性值: content-box,该属性对于盒子尺寸来说,并不会让 ...

  10. centos7安装oracle版本的jdk

    Hadoop机器上的JDK,最好是Oracle的Java JDK,不然会有一些问题,比如可能没有JPS命令. 如果安装了其他版本的JDK,卸载掉!!! 1,查看是否已经安装了jdk java -ver ...