145. Binary Tree Postorder Traversal
题目:
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,也就是当前节点右子树中最左边的元素。下面我们详述一下步骤。
- 依然先做一个root的reference - node,在node非空的情况对树进行遍历。当node.right为空的时候,我们将node.val从链表res头部插入,然后向左遍历左子树
- 假如右子树非空,则我们要找到当前节点在右子树中的succesor,简称succ,一样是两种情况:
- 首次访问succ,此时succ.left = null,我们把succ和当前node连接起来,进行succ.left = node。之后讲node.val从链表res头部插入,向右遍历node.right
- 否则,我们二次访问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的更多相关文章
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- Java for LeetCode 145 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- leetcode 145. Binary Tree Postorder Traversal ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
随机推荐
- linux下挂载iso镜像的方法
新建目录/mnt/cdrom 执行命令 mount /dev/cdrom /mnt/cdrom [root@ocdp1 cdrom]# mount /dev/cdrom /mnt/cdrom moun ...
- kettle Add XML 、 XML Join
1.将文件1.文件2组合成xml文件 文件1 f1;f2;f3 1;张三;24 2;李四;25 文件2 张三;语文;78 张三;数学;88 xml文件 <students> <stu ...
- UITabBar - 深度解剖
for (UIView *tabbarbutton in self.subviews) { // NSLog(@"%@",tabbarbutton); ...
- ORA-22275: invalid LOB locator specified
性能测试20行和20000行的出现错误 20行的字符没有问题,两万行的出行问题如下 [2014-02-11 09:21:03.343665][17694862] Level 0 cicmpub.C: ...
- sicily 1200欢迎提出优化方案
水题来的……我的做法是用a[10]数组表示每个数字出现的次数. 1200. Stick 限制条件 时间限制: 1 秒, 内存限制: 32 兆 题目描述 Anthony has collected a ...
- mysql 乱码问题处理
出现乱码的问题,主要就是因为数据在被处理的过程中,出现了编码和解码不对应造成的.因此解决编码问题的方法也就是在通过让编码和解码的过程能够对应起来就OK了,大学的而是,记得经常搞这个问题,今天又碰到了m ...
- 链表C++模板实现
#include <iostream.h> #include <stdlib.h> //结点模板类 template <typename t1, typename t2& ...
- jQuery按钮复制文本内容
这种方法能保证文本内容被复制到windows剪切板,代码示例是复制url <!doctype html> <html> <head> <meta charse ...
- 动态获取爱奇艺上传视频mp4格式url地址
有时候,在工作中有些客户需要用到视频,我们大家都知道视频是非常的耗费流量的,因此,如果因为项目要求客户单独买台视频服务器是非常划不来的.那么将视频上传到优酷,爱奇艺等视频网站来托管那是一件很好的解决方 ...
- PERL代码摘录
1. 语法与数据结构 #嵌套哈希的赋值和取值 $HashTable{$key} = [@Array] #这个是赋值 @Array = @{ $HashTable{$key} } # 这个是取值 #Pe ...