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?

Subscribe to see which companies asked this question

解答

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* postorderTraversal(struct TreeNode* root, int* returnSize) {
    ], top = -, i = ;
    );
    struct TreeNode *visit = NULL;

     != top||NULL != root){
        while(NULL != root){
            stack[++top] = root;
            root = root->left;
        }
        root = stack[top];
        if(NULL == root->right||visit == root->right){
            return_array[i++] = root->val;
            top--;
            visit = root;
            root = NULL;
        }
        else{
            root = root->right;
        }
    }
    *returnSize = i;
    return return_array;
}

LeetCode OJ 145. Binary Tree Postorder Traversal的更多相关文章

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

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

  2. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  3. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  4. LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)

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

  5. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

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

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

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

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

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

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

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

随机推荐

  1. 关于-RegExp

    // exec() exec() 查找并返回当前的匹配结果,并以数组的形式返回. // test() test() 方法检索字符串中的指定值.返回值是 true 或 false. // compile ...

  2. linux 安装scala

    1. 下载scala 到scala官网下载scala https://www.scala-lang.org/download/,目前最新版本是2.12.8 wget https://downloads ...

  3. IntelliJ IDEA 版本控制器 - Git

    1.下载Git 2.测试Git是否安装成功 3.设置 本机 Git 标识,同时解决未设置标识产生的错误 Successfully created project 'demo' on GitHub, b ...

  4. 通过mapreduce把mysql的数据读取到hdfs

    前面讲过了怎么通过mapreduce把mysql的一张表的数据放到另外一张表中,这次讲的是把mysql的数据读取到hdfs里面去 具体怎么搭建环境我这里就不多说了.参考 通过mapreduce把mys ...

  5. redis参数改进建议

    1.修改stop-writes-on-bgsave-error为no当前配置为yes,分别修改redis.conf和当前实例#redis.confstop-writes-on-bgsave-error ...

  6. python 之 json 与pickle 模块

    序例化:将对象转换为可通过网络传输或可以存储到本地磁盘的数据格式(如:XML.JSON或特定格式的字节串)的过程称为序列化:反之,则称为反序列化. 1.[JSON] import json dic={ ...

  7. WPF去除窗体边框及白色边框

    <Window x:Class="WpfAppFirst.Evaluation" xmlns="http://schemas.microsoft.com/winfx ...

  8. 【Jmeter自学】jmeter实战-其他请求和总结(七)

    FTP测试 步骤:其他的结果树等跟http请求一样  mysql数据库测试 JDBC请求 Jmeter-分布式性能测试 jmeter结果分析:

  9. 逻辑卷(lv)管理(LVM)

    可以灵活改变分区大小.这里的分区叫做lv,lv创建在 vg中,vg由pv组成.pv可以由磁盘创建也可以由物理分区创建.灵活改变分区大小,就是调整lv的大小.lv可以调整的范围受到vg大小的限制,而 v ...

  10. 20165205 2017-2018-2 《Java程序设计》第六周学习总结

    20165205 2017-2018-2 <Java程序设计>第六周学习总结 教材学习内容总结 String类 String对象(常量,对象) 字符串并置(结果仍是常量) 常用方法 len ...