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. 用node.js和webpack做前后端分离的总结

    1.webpack打包的特点 (打包文件到指定地点,修改原文件里的引用路径为打包的地点) 涉及output的path/public path/dev-server里的public path等概念 we ...

  2. win10间歇性的找不到usb设备

    自从安装了win10,感觉掉了一个好大的坑. 比如win10经常找不到usb 设备,有时候过5-6分钟又有了.除了驱动的问题之外,有时候重启一下就好了. 今天又有一个小发现,笔记本为了省电,会把usb ...

  3. C语言中 有符号数、无符号数、整数溢出 (转)

    #include<stdio.h> void main() { int l=-1; unsigned int c=135; printf("%u\n",l+c); } ...

  4. .NET/C#发起GET和POST请求的几种方法

    using System.Net; GET:   1 2 3 var request = (HttpWebRequest)WebRequest.Create("http://www.lead ...

  5. datagrid行内编辑时为datetimebox

    $.extend($.fn.datagrid.defaults.editors, { datetimebox: {// datetimebox就是你要自定义editor的名称 init: functi ...

  6. python 打印调用栈

    import traceback def BBQ(): traceback.print_stack() 引入 traceback 包,在某个函数中执行 traceback.print_stack().

  7. day2作业(基本数据类型,语法)

    #coding:utf-8 '''1.写代码实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!实现用户输入用户名和密码,当用户名为 seven 且 ...

  8. ES6入门声明

    let.var区别点 1.let只在命令所在的代码块中有效. 2.变量一定要先声明在使用,否则会报错,不存在Es5的变量提升(暂时性死区,不存在重复使用). 3.块级作用域中存在let命令,所声明的变 ...

  9. 【Selenium-WebDriver问题篇】Selenium实现元素的拖拽(java版)(转)

    https://blog.csdn.net/u010503127/article/details/51381284 Selenium实现元素的拖拽(java版) [前言] 自从淘宝网登陆页出现滑块验证 ...

  10. [Android]Android布局优化之 ViewStub

    转载请标注:转载于http://www.cnblogs.com/Liuyt-61/p/6602926.html -------------------------------------------- ...