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. EXCEL通俗易懂讲公式(一):sumif,sumifs,countif,countifs

    最近公司招了一批新人,excel基本都是小白阶段,以前用过的也就是画个课程表,没做过什么数据统计和文本计算等工作.因此各种问题都来了,什么vlookup,offset,连条件求和的sumif也不会用, ...

  2. Elasticsearch 学习之配置文件详解

    Elasticsearch配置文件##################### Elasticsearch Configuration Example ##################### # # ...

  3. VMware ESXI5.5 Memories limits resolved soluation.

    在使用VMware ESXI5.5 的时候提示内存限制了,在网上找的了解决方案: 如下文: 1. Boot from VMware ESXi 5.5; 2. wait "Welcome to ...

  4. 微信公众号关联(小游戏 小程序 跳转 盒子 wx.navigateToMiniProgram)

    参考: 公众号关联小程序 关联公众号 关联后,登录小游戏,可在设置-关联设置中看到关联的公众号 在小游戏中使用wx.navigateToMiniProgram wx.navigateToMiniPro ...

  5. -bash: locate: command not found

    部分版本的linux系统使用locate快速查找某文件路径会报以下错误: -bash: locate: command not found 其原因是没有安装mlocate这个包 安装:yum  -y ...

  6. 【CF932F】Escape Through Leaf 启发式合并set维护凸包

    [CF932F]Escape Through Leaf 题意:给你一棵n个点的树,每个点有树形ai和bi,如果x是y的祖先,则你可以从x花费$a_x\times b_y$的费用走到y(费用可以为负). ...

  7. AFNetWork 简单实用demo

    NSString *postUrl = @"http://www.huway.com/api_index?module=event&action=topads"; NSDi ...

  8. AngularJS初始(一)

    什么是AngularJs? angularjs是一个为动态WEB应用设计的结构框架.它能让你使用HTML作为模板语言,通过扩展HTML的语法,让你能更清楚.简洁地构建你的应用组件.它的创新点在于,利用 ...

  9. easyui---基础组件:dialog

    依赖下面两个组件 window linkbutton linbutton组件:就是超链接变成按钮 $(function(){ $("#linkbuttonid").linkbutt ...

  10. Oracle卸载之Win7操作系统下Oracle11g 数据库卸载过程图解

    1.首先停止oracle11g数据库的5个服务 右键“计算机”,在下拉菜单列表中单击“管理”,进入计算机管理器.图解步骤如下: 选择左侧工具栏最后一项“服务和应用程序”,点击进入下拉菜单,单击“服务” ...