问题

  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].

递归方法

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
if(root==null)
return list;
postorderTraversal(root, list);
return list;
}
public void postorderTraversal(TreeNode root, ArrayList<Integer> list){
if(root==null)
return; if(root.left!=null)
postorderTraversal(root.left, list); //先遍历左节点
if(root.right!=null)
postorderTraversal(root.right, list); //再遍历右节点
      list.add(root.val);    //最后遍历根节点
}
}

非递归方法(利用栈)

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>();
if(root==null)
return list;
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<TreeNode> st = new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
if(node.left!=null)
stack.push(node.left);
if(node.right!=null)
stack.push(node.right);
st.push(node);
}
while(!st.isEmpty()){
list.add(st.pop().val);
}
return list;
} }

 

树——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题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

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

  3. [LeetCode] 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: [,,] \ / O ...

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

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

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

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

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

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

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

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

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

    给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...

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

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

随机推荐

  1. 最近使用的两个工具 winscp和xshell

    最近在编译MONO源码时用到了这两个工具,感觉挺好,记录备忘 WINSCP 长这样 用于可视化的查看和操作远程服务器上的文件 xshell用于命令行下操作远程服务器,服务器的同学好多在用这个.命令行是 ...

  2. 最新版本的MySQL的下载和安装(Release: 8.0.12)

    1.打开百度搜索[Myql],或直达官网https://dev.mysql.com/ 2.点选[Download按钮],跳转到下载页面,拉到底部再点选[Community Download]社区版[免 ...

  3. 使用自定义的tstring.h

    UNICODE   控制函数是否用宽字符版本_UNICODE 控制字符串是否用宽字符集 _T("") 根据上述定义来解释字符集 // 在tchar.h中 // tstring.h ...

  4. vue-element-template模板项目使用记录(持续更新)

    1. npm 使用注意事项: a. node.js 使用 v8.16.0 版本,使用 v10 版本会有各种莫名其妙的报错 b. 开箱先改淘宝镜像: npm config set registry ht ...

  5. Arrays.toList工具类

  6. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_5_BufferedWriter_字符缓冲输出流

    使用newLine来换行 同样的效果 println的源码里面其实就用的就是newLine()

  7. Docker image 和 volume 的关系

    image :镜像 虚拟机容器需要加载image才能运行,镜像中打包了构建好服务的运行环境. Docker images are the basis of containers. An Image i ...

  8. Ora01653 :是表空间不足

    解决方案:表空间中增加数据文件: ALTER TABLESPACE 表空间名称ADD DATAFILE 'D:\app\Administrator\oradata\orcl\Ibomis1.dbf' ...

  9. struts2 基础2 类型转换器

    struts2常用常量的定义与意义 每一次请求都会创建一个新的action,所以struts2的action是线程安全的 拆分struts 为应用指定多个struts配置文件 src 下为各应用配置的 ...

  10. mysql恢复备份导出

    MySQL-5.7 备份与恢复   一.备份分类 按介质分类: 物理备份指通过拷贝数据库文件方式完成备份,适用于数据库很大,数据重要且需要快速恢复的数据库. 逻辑备份指通过备份数据库的逻辑结构和数据内 ...