LeetCode解题报告: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?
1.递归算法的递归定义是:
若二叉树为空,则遍历结束;否则
⑴ 后序遍历左子树(递归调用本算法);
⑵ 后序遍历右子树(递归调用本算法) ;
⑶ 访问根结点 。
对有n个结点的二叉树,其时间复杂度均为O(n) 。
List<Integer> postOrder = new ArrayList<Integer>();
public List<Integer> postorderRecursion(TreeNode root) {
if ((root != null) && (root.val != '#')) {
postorderRecursion(root.left);
postorderRecursion(root.right);
postOrder.add(root.val);
}
return postOrder;
}
2.非递归算法
在后序遍历中,根结点是最后被访问的。因此,在遍历过程中,当搜索指针指向某一根结点时,不能立即访问,而要先遍历其左子树,此时根结点进栈。当其左子树遍历完后再搜索到该根结点时,还是不能访问,还需遍历其右子树。所以,此根结点还需再次进栈,当其右子树遍历完后再退栈到到该根结点时,才能被访问。
因此,设立一个状态标志变量tag :{ 0 : 结点暂不能访问;1 : 结点可以被访问}。
其次,设两个堆栈S1、S2 ,S1保存结点,S2保存结点的状态标志变量tag 。S1和S2共用一个栈顶指针。
设T是指向根结点的指针变量,非递归算法是:
若二叉树为空,则返回;否则,令p=T;
⑴ 第一次经过根结点p,不访问:
p进栈S1 , tag 赋值0,进栈S2,p=p->Lchild 。
⑵ 若p不为空,转(1),否则,取状态标志值tag :
⑶ 若tag=0:对栈S1,不访问,不出栈;修改S2栈顶元素值(tag赋值1) ,取S1栈顶元素的右子树,即p=S1[top]->Rchild ,转(1);
⑷ 若tag=1:S1退栈,访问该结点;
直到栈空为止。
List<Integer> postOrder = new ArrayList<Integer>();
public List<Integer> postorderTraversal(TreeNode p) {
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<Boolean> tag = new Stack<Boolean>();
while ((p != null) || !stack.isEmpty()) {
if (p != null) {
stack.push(p);
tag.push(false);
p = p.left;
} else {
boolean visit = tag.pop();
if (visit) {
postOrder.add(stack.pop().val);
} else {
tag.push(true);
p = stack.peek().right;
}
}
}
return postOrder;
}
二叉树的三种遍历递归和非递归实现:递归实现都简单;非递归的前序和中序实现简单,后序采用2个栈来实现(参考严蔚敏的思路,比较容易理解)。代码如下:
import java.util.ArrayList;
import java.util.List;
import java.util.Stack; import javax.swing.text.AbstractDocument.LeafElement; /**
* Definition for binary tree public class TreeNode { int val; TreeNode left;
* TreeNode right; TreeNode(int x) { val = x; } }
*/
public class TreeNodeSolution { public List<Integer> preorderRecursion(TreeNode root) {
List<Integer> preOrder = new ArrayList<Integer>();
if ((root != null) && (root.val != '#')) {
preOrder.add(root.val);
postorderRecursion(root.left);
postorderRecursion(root.right);
}
return preOrder;
} public List<Integer> inorderRecursion(TreeNode root) {
List<Integer> inOrder = new ArrayList<Integer>();
if ((root != null) && (root.val != '#')) {
postorderRecursion(root.left);
inOrder.add(root.val);
postorderRecursion(root.right);
}
return inOrder;
} public List<Integer> postorderRecursion(TreeNode root) {
List<Integer> postOrder = new ArrayList<Integer>();
if ((root != null) && (root.val != '#')) {
postorderRecursion(root.left);
postorderRecursion(root.right);
postOrder.add(root.val);
}
return postOrder;
} public List<Integer> inorderTraversal(TreeNode p) {
List<Integer> inOrder = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
while ((p != null) || !stack.isEmpty()) {
if (p != null) {
stack.push(p);
p = p.left;
} else {
p = stack.pop();
inOrder.add(p.val);
p = p.right;
}
}
return inOrder;
} public List<Integer> preorderTraversal(TreeNode p) {
List<Integer> preOrder = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
while ((p != null) || !stack.isEmpty()) {
if (p != null) {
preOrder.add(p.val);
stack.push(p);
p = p.left;
} else {
p = stack.pop();
p = p.right;
}
}
return preOrder;
} public List<Integer> postorderTraversal(TreeNode p) {
List<Integer> postOrder = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<Boolean> tag = new Stack<Boolean>();
while ((p != null) || !stack.isEmpty()) {
if (p != null) {
stack.push(p);
tag.push(false);
p = p.left;
} else {
boolean visit = tag.pop();
if (visit) {
postOrder.add(stack.pop().val);
} else {
tag.push(true);
p = stack.peek().right;
}
}
}
return postOrder;
} public static void main(String[] args) {
TreeNode t1 = new TreeNode(1);
TreeNode t2 = new TreeNode(2);
TreeNode t3 = new TreeNode(3);
t1.setRight(t2);
t1.setLeft(t3);
System.out.println(new TreeNodeSolution().postorderTraversal(t1));
}
}
LeetCode解题报告:Binary Tree Postorder 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 OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- LeetCode OJ 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- LeetCode题解之Binary Tree Postorder Traversal
1.题目描述 2.问题分析 递归 3.代码 vector<int> postorderTraversal(TreeNode* root) { vector<int> v; po ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
随机推荐
- Oracle11g新特性导致空表不能导出问题
ORACLE 11G在用EXP导出时,发现空表(没有数据或者没有用过的表)不能导出了. 查了一下资料,说是Oracle 11G中有个新特性,当表无数据时,不分配segment,以节省空 ...
- CentOS7安装vim7.4
卸载自带vim yum remove vim-enhanced vim-common 下载vim包 wget ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz ...
- ashx+html+ajax
HTML: $(function() { ajax("NewsList.ashx", function(resText) { document.getElementById(&qu ...
- user.table.column, table.column 或列说明无效
Oracle统计采用别名出错(user.table.column, table.column 或列说明无效) >>>>>>>>>>>& ...
- css中判断IE版本的语句
css中判断IE版本的语句<!--[if gte IE 6]> Only IE 6/+ <![endif]-->: 1. <!--[if !IE]> 除IE外都可识 ...
- Bootstrap 开关(switch)控件需要注意的问题
远程文档地址:http://www.bootcss.com/p/bootstrap-switch/ 先上lz遇到的小坑:自古无图无真相的原则 上面代码注释掉后 就是下面这个图片效果!然后加载顺序也要注 ...
- 【转】Java中equals和==的区别
[转]Java中equals和==的区别 java中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boole ...
- Jquery Datatables(三)
最近在项目中又使用Datatables的一个有趣功能,官网列子如下图: 点击“+”,展开列表,再次点击收缩. 官网的列子点击展开后的数据也是原来行中的数据,这边有了一个想法是否可以利用Ajax去动态加 ...
- 7z 压缩命令行工具
命令行压缩解压一 7z 1) 简介7z,全称7-Zip, 是一款开源软件.是目前公认的压缩比例最大的压缩解压软件.主页:http://www.7-zip.org/中文主页:http://7z.spar ...
- 自定义Operation
1.要自定义一个Operation 首先要创建一个继承于NSOperation的类. 2.在创建好的类的.h文件声明自定义的方法:-(instancetype)initWithDownLoadMess ...