Leetcode: Binary Tree Postorder Transversal
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?
难度:70
recursive方法很直接:
public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
helper(root, res);
return res;
}
private void helper(TreeNode root, ArrayList<Integer> res)
{
if(root == null)
return;
helper(root.left,res);
helper(root.right,res);
res.add(root.val);
}
Iterative 最优做法:
pre-order traversal is root-left-right.
post-order traversal is left-right-root.
We can modify pre-order traversal to be root-right-left, and traverse the tree.
Finally, we reverse the output by the modified pre-order traversal to get post-order traversal.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> res = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode p = root;
while (p!=null || !stack.isEmpty()) {
if (p != null) {
stack.push(p);
res.addFirst(p.val);
p = p.right;
}
else {
TreeNode node = stack.pop();
p = node.left;
}
}
return res;
}
}
第一次Iterative的做法就没有那么strait forward的了,需要额外用一个ArrayList<TreeNode>来记录节点的访问情况
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (root == null) return res;
ArrayList<TreeNode> visited = new ArrayList<TreeNode>();
LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
stack.push(root);
while (root != null || !stack.isEmpty()) {
if (root.left != null && !visited.contains(root.left)) {
stack.push(root.left);
root = root.left;
}
else if (root.right != null && !visited.contains(root.right)) {
stack.push(root.right);
root = root.right;
}
else {
visited.add(root);
res.add(stack.pop().val);
root = stack.peek();
}
}
return res;
}
}
Leetcode: Binary Tree Postorder Transversal的更多相关文章
- [LeetCode] Binary Tree Postorder题解
Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Leetcode Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode——Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- LeetCode: Binary Tree Postorder Traversal [145]
[题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...
- LeetCode Binary Tree Postorder Traversal(数据结构)
题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. /** * Definition for a binary tre ...
随机推荐
- Android NDK学习(1) 简介
转:http://www.cnblogs.com/fww330666557/archive/2012/12/14/2817385.html 一.What is the NDK? The Android ...
- jenkins不安装任何插件时,是什么样的
因为jenkins中插件众多,很多时候大家默认安装的话,会默认安装一堆插件.而很多插件会给jenkins的各个方面去增强它, 一致于不清楚哪些功能是插件提供的,还是jenkins自带的. 所以下面将没 ...
- INTRO: THE DAWN (亡灵序曲) 中独白
As the last ship sailed towards the distant horizon I sat there watching on a rock My mind slowly dr ...
- 统计Java项目的代码行数
Java项目谈论行数多少有点无聊,但是有的时候就想看看一个开源的代码的量级,用Shell命令统计再合适不过了 去掉空行和注释: find . -name "*.java" |xar ...
- 关于Python的集合set
网上那么多说创建集合的语句是: >>>a=set([1,2,3]) python 3.6.3,你们真的能运行吗? 我这里报: Traceback (most recent call ...
- HDU 6312 - Game - [博弈][杭电2018多校赛2]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6312 Problem Description Alice and Bob are playing a ...
- HDU - 5651 xiaoxin juju needs help 逆元模板
http://acm.hdu.edu.cn/showproblem.php?pid=5651 题意:生成回文串.输出所有回文串的可能数. 题解:mod除法会损失高位,用逆元来代替除法,模板如下 ac代 ...
- Hive和Sqoop测试数据
测试数据以Oracle数据库自带scott用户emp和dept表为准: 一.MySQL数据库创建的emp和dept表语法及数据: drop table if exists dept;create ta ...
- python面向对象高级:Mixin多重继承
继上一篇学习笔记:python面向对象的继承与多态,本篇就Mixin扩展类的方法写下学习笔记 Mixin Mixin编程是一种开发模式,是一种将多个类中的功能单元的进行组合的利用的方式,这听起来就像是 ...
- nginx的一些基本功能
1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置:[plain] view plain copy serv ...