题目:

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?

链接: http://leetcode.com/problems/binary-tree-postorder-traversal/

题解:

二叉树后序遍历。 刚接触leetcode时也做过这道题,用了很蠢笨的方法。现在学习discuss里大神们的版本,真的进步很多。下面这个版本是基于上道题目 - 二叉树先序遍历的。由于后序遍历是left -> right -> root,  先序是root -> left -> right, 所以我们改变的只是如何插入结果到 list里,以及被压入栈的先后顺序而已。在这里,pop出的结果要插入到list前部,而且要先把左子树压入栈,其次是右子树。

Time Complexity - O(n),Space Complexity - O(n)。

/**
* 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) {
List<Integer> res = new LinkedList<>();
if(root == null)
return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root); while(!stack.isEmpty()) {
TreeNode node = stack.pop();
if(node != null) {
res.add(0, node.val); // insert at the front of list
stack.push(node.left); // opposite push
stack.push(node.right);
}
} return res;
}
}

二刷:

Java:

/**
* 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) {
List<Integer> res = new LinkedList<>();
if (root == null) return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root); while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (node != null) {
res.add(0, node.val);
stack.push(node.left);
stack.push(node.right);
}
}
return res;
}
}

Recursive:

/**
* 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) {
List<Integer> res = new ArrayList<>();
postorderTraversal(res, root);
return res;
} private void postorderTraversal(List<Integer> res, TreeNode root) {
if (root == null) return;
postorderTraversal(res, root.left);
postorderTraversal(res, root.right);
res.add(root.val);
}
}

三刷:

Java:

Reversed preorder traversal with LinkedList and Stack:

主要使用一个LinkedList和一个stack来辅助我们的遍历。其实顺序和pre-order并没有区别,只是把遍历过的节点值不断从链表头部插入,也就形成了我们的后续遍历。注意处理节点的左节点和右节点时,对栈先压入左节点再压入右节点,之后pop时我们就会先处理右节点。

Time Complexity - O(n),Space Complexity - O(n)。

/**
* 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) {
List<Integer> res = new LinkedList<>();
if (root == null) return res;
Stack<TreeNode> stack = new Stack<>();
TreeNode node = root;
stack.push(node);
while (!stack.isEmpty()) {
node = stack.pop();
res.add(0, node.val);
if (node.left != null) stack.push(node.left);
if (node.right != null) stack.push(node.right);
}
return res;
}
}

Recursive:

Time Complexity - O(n),Space Complexity - O(n)。

/**
* 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) {
List<Integer> res = new ArrayList<>();
postorderTraversal(res, root);
return res;
} private void postorderTraversal(List<Integer> res, TreeNode root) {
if (root == null) return;
postorderTraversal(res, root.left);
postorderTraversal(res, root.right);
res.add(root.val);
}
}

Morris Traversal:

三刷终于学习了Morris Traversal。这里的Morris Traversal也使用的是Reversed Preorder traversal with LinkedList。也就是使用类似与Preorder traversal类似的代码,以及一个LinkedList,每次从表头插入结果。

因为Postorder和Preorder的对应关系,这里与Preorder Morris Traversal不同的就是,我们要找的不是左子树的predecessor,而是右子树中当前节点的successor,也就是当前节点右子树中最左边的元素。下面我们详述一下步骤。

  1. 依然先做一个root的reference - node,在node非空的情况对树进行遍历。当node.right为空的时候,我们将node.val从链表res头部插入,然后向左遍历左子树
  2. 假如右子树非空,则我们要找到当前节点在右子树中的succesor,简称succ,一样是两种情况:
    1. 首次访问succ,此时succ.left = null,我们把succ和当前node连接起来,进行succ.left = node。之后讲node.val从链表res头部插入,向右遍历node.right
    2. 否则,我们二次访问succ,这时succ.left = node,我们做一个还原操作,设succ.left = null,然后向左遍历node.left。因为node.val已经处理过,所以不要重复处理node.val.

Time Complexity - O(n),Space Complexity - O(1)。

/**
* 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) {
List<Integer> res = new LinkedList<>();
TreeNode node = root, succ = null;
while (node != null) {
if (node.right == null) {
res.add(0, node.val);
node = node.left;
} else {
succ = node.right;
while (succ.left != null && succ.left != node) {
succ = succ.left;
}
if (succ.left == null) {
succ.left = node;
res.add(0, node.val);
node = node.right;
} else {
succ.left = null;
node = node.left;
}
}
}
return res;
}
}

Reference:

144. Binary Tree Preorder Traversal

https://leetcode.com/discuss/9736/accepted-code-with-explaination-does-anyone-have-better-idea

https://leetcode.com/discuss/71943/preorder-inorder-and-postorder-iteratively-summarization

https://leetcode.com/discuss/21995/a-very-concise-solution

https://leetcode.com/discuss/36711/solutions-iterative-recursive-traversal-different-solutions

145. Binary Tree Postorder Traversal的更多相关文章

  1. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  2. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  3. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

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

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

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

  5. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  6. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  7. Java for LeetCode 145 Binary Tree Postorder Traversal

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

  8. leetcode 145. Binary Tree Postorder Traversal ----- java

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

  9. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

随机推荐

  1. ios Swift 资源池

    Swift入门教程: http://www.cocoachina.com/applenews/devnews/2014/0604/8661.html Swift视频教程: http://www.coc ...

  2. Android系统简介(中):系统架构

    Android的系统架构栈分为4层,从上往下分别是Applications.Application framework.Libraries  & Android Runtime.Linux  ...

  3. 14_Response对象

    [简述] Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象和代表响应的response对象. request和response对象既然代表请求和响应 ...

  4. tomcat错误信息解决方案【严重:StandardServer.await: create[8005]

    1.独立运行的tomcat.exe没有关闭,关闭tomcat图标并结束掉tomcat进程.(我是这个原因,在开始菜单里找到tomcat,然后stop它) 2.安装了其他的软件占用了8080端口,tom ...

  5. 循环/loop 结构/structure

    1.Shell loop 2.C++/CPlusPlus ①.std::for_each ②.for loop ③.Iterator library 3.Python Loop ①.Python.or ...

  6. bash 脚本

    一.英文版 1.Advanced Bash-Scripting Guide 二.中文 1.BashGuide for Beginners 中文版

  7. TreeSet集合

    TreeSet集合 TreeSet集合是一个依靠TreeMap实现的有序集合,内部存储元素是自动按照自然排序进行排列,所以如果想要保留存储时的顺序,那么就不建议使用TreeSet. TreeSet继承 ...

  8. Head First 设计模式系列之二----备忘录模式(java版)

    申明:这几天无意中关注到备忘录模式,比较陌生回家一番参考书,只在附录里记录了该模式.后来在园子里有发现了有专门写设计模式的博客,并且写的也得牛逼.附上链接 http://www.cnblogs.com ...

  9. CentOS 开启GD库

    在php.ini 中没有找到"extension=php_gd2.dll"这行代码,这是因为CentOS一般没有预装GD库. 解决办法: 1.在线安装GD库 yum -y inst ...

  10. 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(三)

    jar包完成后,剩下就是要构建ANE包来供实际程序调用. 首先要建两个Flex库项目, default那个是官方建议加上的,仅用于不在真实环境下编译调试的时候有个默认接口不至于调用不成功报错,项目结构 ...