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 ...
随机推荐
- Java Stax操作XML简介
使用stax操作xml 非常的简单,它的读取过程像是一个光标在移动.针对不同的节点做不同的处理. 先看一个基于光标的模型处理xml: public class StaxTest { @Test pub ...
- Windows Server 2008 R2中关闭“IE增强的安全配置”
当在Windows Sever 2008 R2中运动IE8的时候会发现默认情况下IE启用了增强的安全配置,为了方便而且是在内网的情况下我们可以关闭IE8的增强安全配置,操作很简单如下步骤. 一,以本机 ...
- wget下载网站整个目录
wget -r -p -np -k -P ./data/ http://example.com/eg/ 具体参数: -P 表示下载到哪个目录 -r 表示递归下载 -np 表示不下载旁站连接 -k 表示 ...
- bash登录式shell(完全切换)与非登陆式shell(不完全切换)区别
1.以登录式shell切换用户 su - username 登录式shell读取配置文件及其顺序: /etc/profile /etc/profile.d/*.sh ~/.bash_profile ~ ...
- Mybaitis配置总结
在mybatis-config.xml中配置分页插件,插件配置必须放在mapper前面 <plugins> <plugin interceptor="com.rrtong. ...
- Windows Python 2.7 安装 Numpy
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/4846093. ...
- 微信上传图文消息invalid media_id hint,thumb_media_id怎么获取
微信上传图文消息thumb_media_id, thumb_media_id怎么获取, 微信群发图文消息invalid media_id hint, 微信群发图文消息40007, 40007,inva ...
- Java-struts2 之值栈问题
这里是根据一个小项目,将数据库的值查出来,然后在页面前台进行遍历的方法 放入值的几种方式: Struts2的三种存值取值的方式 值栈: 栈上下文: ActionContext: package com ...
- ASCII 码表对照
ASCII码表 ASCII码大致可以分作三部分组成.第一部分是:ASCII非打印控制字符第二部分是:ASCII打印字符:第三部分是:扩展ASCII打印字符 第一部分:ASCII非打印控制字符表 ASC ...
- C#当中的多线程_任务并行库(上)
复习: 第三章内容中我们提到了三种异步编程模型,这里简单复习一下,分别如下 1.APM(异步编程模式):形如Beginxxx,Endxxx. 2.EAP(基于事件的异步编程模式):这个我们在.net中 ...