Binary Tree Postorder Traversal leetcode java
题目:
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 public void helper(TreeNode root, ArrayList<Integer> re){
2 if(root==null)
3 return;
4
5 helper(root.left,re);
6 helper(root.right,re);
7 re.add(root.val);
8 }
9 public ArrayList<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> re = new ArrayList<Integer>();
if(root==null)
return re;
helper(root,re);
return re;
}
非递归方法代码:
引用自Code ganker:http://blog.csdn.net/linhuanmars/article/details/22009351
“接下来是迭代的做法,本质就是用一个栈来模拟递归的过程,但是相比于Binary
Tree Inorder Traversal和Binary
Tree Preorder Traversal,后序遍历的情况就复杂多了。我们需要维护当前遍历的cur指针和前一个遍历的pre指针来追溯当前的情况(注意这里是遍历的指针,并不是真正按后序访问顺序的结点)。具体分为几种情况:
(1)如果pre的左孩子或者右孩子是cur,那么说明遍历在往下走,按访问顺序继续,即如果有左孩子,则是左孩子进栈,否则如果有右孩子,则是右孩子进栈,如果左右孩子都没有,则说明该结点是叶子,可以直接访问并把结点出栈了。
(2)如果反过来,cur的左孩子是pre,则说明已经在回溯往上走了,但是我们知道后序遍历要左右孩子走完才可以访问自己,所以这里如果有右孩子还需要把右孩子进栈,否则说明已经到自己了,可以访问并且出栈了。
(3)如果cur的右孩子是pre,那么说明左右孩子都访问结束了,可以轮到自己了,访问并且出栈即可。
算法时间复杂度也是O(n),空间复杂度是栈的大小O(logn)。实现的代码如下(代码引用自:http://www.programcreek.com/2012/12/leetcode-solution-of-iterative-binary-tree-postorder-traversal-in-java/):”
1 public ArrayList<Integer> postorderTraversal(TreeNode root) {
2
3 ArrayList<Integer> lst = new ArrayList<Integer>();
4
5 if(root == null)
6 return lst;
7
8 Stack<TreeNode> stack = new Stack<TreeNode>();
9 stack.push(root);
TreeNode prev = null;
while(!stack.empty()){
TreeNode curr = stack.peek();
// go down the tree.
//check if current node is leaf, if so, process it and pop stack,
//otherwise, keep going down
if(prev == null || prev.left == curr || prev.right == curr){
//prev == null is the situation for the root node
if(curr.left != null){
stack.push(curr.left);
}else if(curr.right != null){
stack.push(curr.right);
}else{
stack.pop();
lst.add(curr.val);
}
//go up the tree from left node
//need to check if there is a right child
//if yes, push it to stack
//otherwise, process parent and pop stack
}else if(curr.left == prev){
if(curr.right != null){
stack.push(curr.right);
}else{
stack.pop();
lst.add(curr.val);
}
//go up the tree from right node
//after coming back from right node, process parent node and pop stack.
}else if(curr.right == prev){
stack.pop();
lst.add(curr.val);
}
prev = curr;
}
return lst;
}
Binary Tree Postorder Traversal leetcode java的更多相关文章
- Binary Tree Preorder Traversal leetcode java
题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bina ...
- Binary Tree Inorder Traversal leetcode java
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...
- Binary Tree Postorder Traversal --leetcode
原题链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题目大意:后序遍历二叉树 解题思路:后序遍历二叉树的步骤: ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
随机推荐
- PHP isset和empty 详细比较
找了几篇博文,这应该是说的最清楚的.链接如下:http://blog.chinaunix.net/uid-25311424-id-3966622.html
- Wannafly挑战赛9 A - 找一找
链接:https://www.nowcoder.com/acm/contest/71/A来源:牛客网 题目描述 给定n个正整数,请找出其中有多少个数x满足:在这n个数中存在数y=kx,其中k为大于1的 ...
- jQuery的data() 和 h5 的 dataset()
作用:获取自定义属性 命名规则:驼峰命名法 data-user==>user data-user-name==>userName 区别:jQuery:操作内存 h5: 操作DOM j ...
- Tensorflow学习:(一)tensorflow框架基本概念
一.Tensorflow基本概念 1.使用图(graphs)来表示计算任务,用于搭建神经网络的计算过程,但其只搭建网络,不计算 2.在被称之为会话(Session)的上下文(context)中执行图 ...
- win10怎么修改svn的用户和密码
win10怎么修改svn的用户和密码(一般为默认),其他的系统也差不多 方法/步骤 1.方法一: 1.双击我的电脑在c盘找到auth文件夹 C:\Users\系统帐户名\AppData\Roaming ...
- 网络图片嗅探工具driftnet
网络图片嗅探工具driftnet 图片是网络数据传输的重要内容.Kali Linux内置了一款专用工具drifnet.该工具可以支持实时嗅探和离线嗅探.它可以从数据流中提取JPEG和GIF这两种网 ...
- requests https访问错误SSLError: certificate verify failed 及InsecureRequestWarning处理办法
转自: https://blog.csdn.net/mighty13/article/details/78076258?locationNum=3&fps=1 在使用requests访问某网站 ...
- POJ 1904 King's Quest tarjan
King's Quest 题目连接: http://poj.org/problem?id=1904 Description Once upon a time there lived a king an ...
- PHP self与static区别
this,static和self. self和this还是很好区分的,可是self和static就很糊涂了,两者都能调用静态的方法和属性,看似使用上没有什么太大的分别,但是实际上分别很大,先来看下面这 ...
- Struts2 JSONObject的使用
一.jar包 使用之前必须引入所须要的jar包,这里包含Struts2和JSONObject各自所必须的 Struts2: commons-fileupload-1.2.1.jarcommons-io ...