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的更多相关文章

  1. [LeetCode] Binary Tree Postorder题解

    Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...

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

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

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

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

  4. Leetcode Binary Tree Postorder Traversal

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

  5. [Leetcode] Binary tree postorder traversal二叉树后序遍历

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

  6. [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索

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

  7. LeetCode——Binary Tree Postorder Traversal

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

  8. LeetCode: Binary Tree Postorder Traversal [145]

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

  9. LeetCode Binary Tree Postorder Traversal(数据结构)

    题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. /** * Definition for a binary tre ...

随机推荐

  1. NC 的高级应用

    高级用法: (1)作攻击程序用,例子: 格式1:type.exe c:\exploit.txt|nc -nvv 192.168.x.x 80 格式2:nc -nvv 192.168.x.x 80 &l ...

  2. Elasticsearch学习之深入搜索五 --- phrase matching搜索技术

    1. 近似匹配 什么是近似匹配,两个句子 java is my favourite programming language, and I also think spark is a very goo ...

  3. 题目1458:汉诺塔III(不一样的汉诺塔递归算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1458 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  4. 用CornerStone配置SVN,HTTP及svn简单使用说明

    转载 http://my.oschina.net/joanfen/blog/194491 一.下载地址 CornerStoneV2.6:http://pan.baidu.com/s/1qWEsEbM密 ...

  5. 如何使用Gradle的maven-publish将jar包或者war包上传到nexus仓库

    首先,在build.gradle里边声明依赖maven-publish插件: apply plugin: 'maven-publish' 然后,配置项目的信息和和nexus的信息: publishin ...

  6. .NET中的三种Timer的区别和用法(收集)

    最近正好做一个WEB中定期执行的程序,而.NET中有3个不同的定时器.所以正好研究研究.这3个定时器分别是: 1.实现按用户定义的时间间隔引发事件的计时器.此计时器最宜用于 Windows 窗体应用程 ...

  7. CentOS安装php及其扩展

    列出所有的可安装的软件包 yum list | grep php56w* | grep redis 安装php及其扩展 yum install  -y php56w php56w-mysql php5 ...

  8. SQL已存在则更新不存在则插入

    不废话,下代码. replace into T_Life_UMessage(message_id,account,isread,isdelete)values(?,?,1,1) 意思是若不存在则插入要 ...

  9. Pointer Lock

    Pointer Lock API 指针锁定(以前叫做 鼠标锁定) 提供了一种输入方法,这种方法是基于鼠标随着时间推移的运动的(也就是说,deltas),而不仅是鼠标光标的绝对位置.通过它可以访问原始的 ...

  10. mysql bin-logrow模式,base64转正常sql

    可以通过以下命令查看日志是否开启查看 show global variables like '%log%'; 当bin-log的模式设置为row时 不仅日志长得快 , 并且查看执行的sql时 , 也稍 ...