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. sencha touch 2.3.1 list emptyText不显示

    如图所示,有时候没有取到任何的数据. 那么我们就需要显示没有获取到内容这一类提示,显示内容通常通过emptyText这个属性来配置. 但是在sencha touch 2.3.1之中有可能会出问题,所以 ...

  2. Docker关联使用的一些工具:Clip名字服务(转载)

    Clip名字服务 Clip(http://blog.puppeter.com/read.php?7)是一个名字服务C/S架构,它将传统的IP管理维度替换为名字服务即有意义可记忆的String.Clip ...

  3. Linux下openoffice与SWFtools的安装

    第一部份:OpenOffice的安装 1.Openoffice的官网:http://www.openoffice.org/download/index.html 选择Linux 64-bit(x860 ...

  4. 【CF888E】Maximum Subsequence 折半搜索

    [CF888E]Maximum Subsequence 题意:给你一个序列{ai},让你从中选出一个子序列,使得序列和%m最大. n<=35,m<=10^9 题解:不小心瞟了一眼tag就一 ...

  5. React 属性和状态的一些总结

    一.属性 1.第一种使用方法:键值对 <ClaaNameA name = “Tom” /> <ClaaNameA name = {Tom} /> <ClaaNameA n ...

  6. Flask详解

    Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后 ...

  7. ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds      Me ...

  8. POJ_2019_Cornfields

    Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7444   Accepted: 3609 Descri ...

  9. hadoop完全分布式安装部署-笔记

    规划: [hadoop@db01 ~]$ cat /etc/hosts127.0.0.1   localhost localhost.localdomain localhost4 localhost4 ...

  10. Metricbeat 的使用

    目标 统计并展示系统的信息 cpu, 内存等 (当然metricbeat能收集的信息种类还很多) 前提 版本: 5.x 已经安装了ELK (elasticsearch, logstash (可选), ...