给定一棵二叉树,返回其节点值的后序遍历。
例如:
给定二叉树 [1,null,2,3],
   1
    \
     2
    /
   3
返回 [3,2,1]。
注意: 递归方法很简单,你可以使用迭代方法来解决吗?
详见:https://leetcode.com/problems/binary-tree-postorder-traversal/description/

Java实现:

递归实现:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public List<Integer> postorderTraversal(TreeNode root) {
  12. List<Integer> res=new ArrayList<Integer>();
  13. if(root==null){
  14. return res;
  15. }
  16. return postorderTraversal(root,res);
  17. }
  18. private List<Integer> postorderTraversal(TreeNode root,List<Integer> res){
  19. if(root==null){
  20. return res;
  21. }
  22. postorderTraversal(root.left,res);
  23. postorderTraversal(root.right,res);
  24. res.add(root.val);
  25. return res;
  26. }
  27. }

非递归实现:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public List<Integer> postorderTraversal(TreeNode root) {
  12. List<Integer> res=new ArrayList<Integer>();
  13. if(root==null){
  14. return res;
  15. }
  16. Stack<TreeNode> stk1=new Stack<TreeNode>();
  17. Stack<TreeNode> stk2=new Stack<TreeNode>();
  18. stk1.push(root);
  19. while(!stk1.isEmpty()){
  20. root=stk1.pop();
  21. stk2.push(root);
  22. if(root.left!=null){
  23. stk1.push(root.left);
  24. }
  25. if(root.right!=null){
  26. stk1.push(root.right);
  27. }
  28. }
  29. while(!stk2.isEmpty()){
  30. res.add(stk2.pop().val);
  31. }
  32. return res;
  33. }
  34. }

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 二叉树的后序遍历

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

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

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

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

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

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

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

  6. lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历

    题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...

  7. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

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

  8. 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)

    这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

  9. Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历

    给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...

随机推荐

  1. mybatis xml文件解析

    1 parameterType 如果参数只有一个,比如一个id,即int类型的id,那么parameterType直接是int. 如果参数有多个,那么就用表中一行对应的类,默认是类的名字和表中列的名字 ...

  2. linux下的文件和文件夹的权限问题

    1 文件和文件夹的权限 文件和文件夹的权限设置的根本目的是控制人对它们的访问. 2 用户分类 本文件的拥有者.本文件所属的grou.其它用户. 3 也就是说 在读写文件或者文件夹时,要看看自己是属于哪 ...

  3. DELPHI中的消息处理机制(三种消息处理方法的比较,如何截断消息)

    DELPHI中的消息处理机制 Delphi是Borland公司提供的一种全新的WINDOWS编程开发工具.由于它采用了具有弹性的和可重用的面向对象Pascal(object-orientedpasca ...

  4. C++中的const完全解析

    1. const修饰普通变量和指针 const修饰变量,一般有两种写法:const TYPE value;TYPE const value; 这两种写法在本质上是一样的.它的含义是:const修饰的类 ...

  5. VC实现趋势图绘制

    本文参考pudn上一个完整工程,在pudn搜索“50815867CurveDrawing”即可找到源代码.   上图是使用VS2010重写了该软件后的效果图,下面再贴出关键代码: // Plot.cp ...

  6. Spark理论学习笔记(一)

    1.调度 分为FIFO和FAIR两种模式 创建调度池:sc.setLocalProperty("spark.scheduler.pool", "pool6") ...

  7. XMLHttp.send()不传参时必须传null吗?

    xmlhttp的send是传递参数用的,但是只有在使用post方式提交请求的时候才有用如下:xmlhttp.open("post",url,true); ...xmlhttp.se ...

  8. linux初级学习笔记三:linux操作系统及常用命令,及如何复制和移动文件!(视频序号:02_4)

    本节学习的命令:cp,mv,install,du,read 本节学习的技能:文件的移动与复制 cp( copy):复制和移动文件 cp SRC DEST -r:递归复制一个目录及其目录中的所有文件 - ...

  9. html5--6-11 CSS选择器7--伪类选择器

    html5--6-11 CSS选择器7--伪类选择器 #E:target 选择当前活动的锚点元素. 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候, ...

  10. Python中的sort() key含义

    sorted(iterable[, cmp[, key[, reverse]]]) iterable.sort(cmp[, key[, reverse]]) 参数解释: (1)iterable指定要排 ...