LeetCode: Binary Tree Postorder Traversal 解题报告
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 解题报告的更多相关文章
- LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- 【LeetCode】144. Binary Tree Preorder Traversal 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】590. N-ary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 相似题目 参考资料 日期 题目地址:htt ...
- LeetCode 590 N-ary Tree Postorder Traversal 解题报告
题目要求 Given an n-ary tree, return the postorder traversal of its nodes' values. 题目分析及思路 题目给出一棵N叉树,要求返 ...
- Leetcode Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
随机推荐
- Nginx+FastCGI运行原理(一)
1 实战Nginx与PHP(FastCGI)的安装.配置与优化 1.1 什么是 FastCGI FastCGI是一个可伸缩地.高速地在HTTP server和动态脚本语言间通信的接口.多数流行的HTT ...
- 进阶之路(基础篇) - 003 I/O的模拟的读取
/********************************* 代码功能:读取某引脚的模拟量串口返回数据 使用函数: analogRead(引脚号); //调用10位AD 创作时间:2016*1 ...
- HDU 4632 Palindrome subsequence (区间DP)
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
- IIS7虚拟目录出现HTTP错误500.19(由于权限不足而无法读取配置文件)的解决方案
今天在window7上配置asp.net网站,但是访问总是提示 错误摘要HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该页的相关配置数据无效.详细 ...
- Oracle 12C -- ADRCI查看DDL日志
$ adrci ADRCI: Release - Production on Tue Nov :: Copyright (c) , , Oracle and/or its affiliates. Al ...
- 解决tomcat服务器下,只能通过localhost,而不能通过127.0.0.1或者本地ip地址访问的问题
今天在tomcat上部署了一个web应用以后,发现用localhost的方式来访问应用是正常的,但是换成127.0.0.1或者是本地的ip地址来访问,确出现访问不了的情况.之前想是不是防火墙的问题,于 ...
- 转: FFmpeg功能命令汇总
原文: FFmpeg功能命令汇总 前言 如此强大的FFmpeg,能够实现视频采集.视频格式转化.视频截图.视频添加水印.视频切片.视频录制.视频推流.更改音视频参数功能等.通过终端命令如何实现这些功能 ...
- kafak manager + zookeeper + kafka 消费队列快速清除
做性能测试时,kafka消息队列比较长,让程序自己消费完毕需要等待很长时间.就需要快速清理kafka队列 清理方式把 这kafak manager + zookeeper + kafka 这些应用情况 ...
- js关闭当前页面和给子页面的对象赋值
代码如下: function saveData(){ //给父页面的对象赋值 frameElement.api.opener.document.getElementById("userNam ...
- xp中使用grubdos安装ubuntu13.04
http://www.cnblogs.com/ggjucheng/archive/2012/08/18/2645916.html 根据以上帖子安装ubuntu13.04 当重启,进入ubuntu in ...