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. PostgreSQL手动主从切换

    主从切换操作: 1>主库宕机或者测试主备切换情况下停掉主库:systemctl stop postgres 从库会报日志错误信息:[root@db02 /]# cd /var/postgresq ...

  2. HBase常用操作命令

    HBase常用操作命令 1.进入HBase脚本客户端 #hbase shell #进入HBase脚本客户端 > whoami    #查看当前登录用户 > status           ...

  3. Unity中进程间通信——使用异步Socket

    开发Unity项目过程中,即时通信功能来完成服务器与客户端自定义的数据结构封装. 如果要序列化和数据封装参考:Unity3D之C#用Socket传数据包 蓝鸥3G封装的类 客户端脚本ClientScr ...

  4. GRUB2 命令行使用笔记

    在GRUB界面按C可进入命令行模式,学会命令行模式有助于玩转单机多OS. 磁盘描述规则: hd0,0 表示第1硬盘第1分区 help 显示帮助(内容会比这里全,此处只做几条常用命令介绍) cat 命令 ...

  5. selenium在scrapy中的应用

    引入 在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生,如果直接使用scrapy对其url发请求,是绝对获取不到那部分动态加载出来的数据值.但是通过观察我们会发现 ...

  6. for循环循环时间

    )) { Console.WriteLine(dt); } ("2011-5-5")       按需求定义 AddDays函数,   一天一天的增长

  7. solr使用cursorMark做深度分页

    深度分页 深度分页是指给搜索结果指定一个很大的起始位移. 普通分页在给定一个大的起始位移时效率十分低下,例如start=1000000,rows=10的查询,搜索引擎需要找到前1000010条记录然后 ...

  8. 2-scala集合

    1.集合的数据结构: array.seq .set .map(映射) 2.集合分类 集合分为可变集合和不可变集合(除元组外,元组没有可变和不可变) scala.collection.immutable ...

  9. nodejs开篇基础<①>

    1.安装相关 //安装brew Homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/insta ...

  10. C# .NET Web Api 保存Session

    Global.asax中添加: public override void Init() { this.PostAuthenticateRequest += (sender, e) => Http ...