Binary Tree Postorder Traversal

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?

Show Tags
Have you met this question in a real interview? Yes  No
Discuss

SOLUTION 1:

递归解法

 public List<Integer> postorderTraversal1(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
dfs(root, ret);
return ret;
} // Solution 1: rec
public void dfs(TreeNode root, List<Integer> ret) {
if (root == null) {
return;
} dfs(root.left, ret);
dfs(root.right, ret);
ret.add(root.val);
}

SOLUTION 2:

/**
     *  后序遍历迭代解法
     *  http://www.youtube.com/watch?v=hv-mJUs5mvU
     *  http://blog.csdn.net/tang_jin2015/article/details/8545457
     *  从左到右的后序 与从右到左的前序的逆序是一样的,所以就简单喽! 哈哈
     *  用另外一个栈进行翻转即可喽
     */

 // Solution 2: iterator
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
if (root == null) {
return ret;
} Stack<TreeNode> s = new Stack<TreeNode>();
Stack<Integer> out = new Stack<Integer>(); s.push(root); while (!s.isEmpty()) {
TreeNode cur = s.pop();
out.push(cur.val); if (cur.left != null) {
s.push(cur.left);
} if (cur.right != null) {
s.push(cur.right);
}
} while (!out.isEmpty()) {
ret.add(out.pop());
} return ret;
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/PostorderTraversal.java

LeetCode: Binary Tree Postorder Traversal 解题报告的更多相关文章

  1. LeetCode: Binary Tree Inorder Traversal 解题报告

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  2. LeetCode: Binary Tree Preorder Traversal 解题报告

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  3. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

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

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

  5. 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  6. 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】590. N-ary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 相似题目 参考资料 日期 题目地址:htt ...

  8. LeetCode 590 N-ary Tree Postorder Traversal 解题报告

    题目要求 Given an n-ary tree, return the postorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返 ...

  9. Leetcode Binary Tree Postorder Traversal

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

随机推荐

  1. MATLAB 不能保存变量问题及解决办法

    在使用matlab保存结构体.元胞数组等等的变量时,matlab总是提示 警告: 未保存变量 'session'.对于大于 2GB 的变量,请使用 MAT 文件版本 7.3 或更高版本.  问题如下: ...

  2. fwrite()的返回值随着格式的不同返回值也不同;

    常用地函数fwrite fwrite()的返回值随着格式的不同返回值也不同: 也是最近涉及到代码才注意到的,汗!!! 转载了一篇文章来说明这个问题:文章地址:http://blog.csdn.net/ ...

  3. Jetty使用内存过大的解决方案

    之前用Jetty做过一个消息通知服务器,主要功能就是其他各个子系统如果有需要push给客户端消息的就把这个消息发给我的Server,我用WebSocket来推送给客户端~ 程序上线一段时间之后运维工程 ...

  4. ubuntu下制作u盘启动盘

    ubuntu12.04下成功制作了ubuntu13.10 U盘启动盘. 成功 ubuntu14.04下成功制作了centos.7 U盘启动盘.成功 1.安装u盘制作工具unetbootin sudo ...

  5. C# winform DataGridView 常见属性

    C# winform DataGridView 属性说明① 取得或者修改当前单元格的内容 ② 设定单元格只读 ③ 不显示最下面的新行 ④ 判断新增行 ⑤ 行的用户删除操作的自定义 ⑥ 行.列的隐藏和删 ...

  6. 【Spring】spring的7个模块

    Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架. Spring ...

  7. 【Spring】SpringMVC中浅析数据的传递方式

    包括了基本数据类型的传递和 Date数据类型的传递.关于SpringMVC的配置可以参见基于注解实现SpringMVC+MySQL 假设有表单页面如下: <h1>登录</h1> ...

  8. MySQL的timestamp类型自动更新问题

    今天建了一个表,里面有一个列是timestamp类型,我本意是在数据更新时,这个字段的时间能自动更新.岂知对这个类型的值还不甚了解,导致出错.发现这个字段只是在这行数据建立的时候有值,在更新的却无变化 ...

  9. Ios开发中UILocalNotification实现本地通知实现提醒功能

    这两天在做一个日程提醒功能,用到了本地通知的功能,记录相关知识如下: 1.本地通知的定义和使用: 本地通知是UILocalNotification的实例,主要有三类属性: scheduled time ...

  10. css中position:fixed实现div居中

    上下左右 居中 代码如下 复制代码 div{ position:fixed; margin:auto; left:0; right:0; top:0; bottom:0; width:200px; h ...