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

Example:

  1. Input: [1,null,2,3]
  2. 1
  3. \
  4. 2
  5. /
  6. 3
  7.  
  8. Output: [3,2,1]

Follow up: Recursive solution is trivial, could you do it iteratively?

---------------------------------------------------------------------------------------------------------------------

leetcode 144. Binary Tree Preorder Traversal类似。

emmm,这个题是hard难度题,大概是在迭代上是不好写吧,不过递归是很容易写的。

C++代码:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. vector<int> postorderTraversal(TreeNode* root) {
  13. vector<int> vec;
  14. postorder(root,vec);
  15. return vec;
  16. }
  17. void postorder(TreeNode *root,vector<int> &vec){
  18. if(!root) return;
  19. postorder(root->left,vec);
  20. postorder(root->right,vec);
  21. vec.push_back(root->val);
  22. }
  23. };

(二叉树 递归) leetcode 145. Binary Tree Postorder Traversal的更多相关文章

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

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

  2. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

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

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

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

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

  5. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

  6. Java for LeetCode 145 Binary Tree Postorder Traversal

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

  7. leetcode 145. Binary Tree Postorder Traversal ----- java

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

  8. (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal

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

  9. Leetcode#145 Binary Tree Postorder Traversal

    原题地址 递归写法谁都会,看看非递归写法. 对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子.所以,最直 ...

随机推荐

  1. js 格林威治时间转正常格式并兼容ios

    function timeChange(time) { var date = time.substr(0, 10); //年月日 var hours = time.substring(11, 13); ...

  2. 基于Xamarin Android实现的简单的浏览器

    最近做了一个Android浏览器,当然功能比较简单,主要实现了自己想要的一些功能……现在有好多浏览器为什么还要自己写?当你使用的时候总有那么一些地方不如意,于是就想自己写一个. 开发环境:Xamari ...

  3. 基于Html5 Plus + Vue + Mui 移动App 开发(二)

    基于Html5 Plus + Vue + Mui 移动App 开发(二) 界面效果: 本页面采用Html5 Plus + Vue + Mui 开发移动界面,本页面实现: 1.下拉刷新.上拉获取更多功能 ...

  4. 一个GIS开源工具集架构的总结

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 最近由团队HWG主导的GIS开源工具集基本告一段落,该项目虽然 ...

  5. OpenCL——把vector变成scalar

    https://stackoverflow.com/questions/46556471/how-may-i-convert-cast-scalar-to-vector-and-vice-versa- ...

  6. ThinkPad 安装 Ubuntu 18.10 系统 -- 高分屏各项配置与Nvdia独显驱动

    索引: 目录索引 一.机器概述 1.屏幕:14'' 2.分辨率:1920*1080 3.显卡:Intel 核显  &  Nvidia  GeForce 940MX 独显 ,双显卡 4.其它硬件 ...

  7. 20年硅谷技术牛人到访DataPipeline谈:技术如何与业务平衡发展

    导读:技术人员的常态是“左手支持业务签单,右手提升系统性能”,却经常陷入技术和业务该如何平衡发展的困惑?今天,且听一位硅谷牛人分享他的平衡之道. 以个人名誉申请31个国内外技术和产品专利,中国最佳CT ...

  8. Python正则表达式很难?一篇文章搞定他,不是我吹!

    1. 正则表达式语法 1.1 字符与字符类 1 特殊字符:.^$?+*{}| 以上特殊字符要想使用字面值,必须使用进行转义 2 字符类 1. 包含在[]中的一个或者多个字符被称为字符类,字符类在匹配时 ...

  9. mysql表与表之间数据的转移

    1.相同表结构 INSERT INTO table1 SELECT * FROM table2; 2.不同表结构 INSERT INTO table1(filed1,...,filedn) SELEC ...

  10. cent os 7 与cent os 6区别

    原文地址:https://www.cnblogs.com/Csir/p/6746667.html 前言 centos7与6之间最大的差别就是初始化技术的不同,7采用的初始化技术是Systemd,并行的 ...