LeetCode145 Binary Tree Postorder Traversal 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].
解题:
递归的还是和前面中序和先序一样。仅仅是交换一下顺序而已
public static List<Integer> result=new ArrayList<Integer>();
public static List<Integer> postorderTraversal(TreeNode root) { if(root!=null)
{
postorderTraversal(root.right);
postorderTraversal(root.left);
result.add(root.val);
}
return result; }
迭代的略微复杂一些 整体上和前面的解法是一样的 只是这边要先訪问左右节点,相同还是以左节点作为主线,只是这里要添加一个栈记录每一个节点的右节点是否已经被訪问过。仅仅有当左右节点都被訪问的前提下才干訪问根节点
public static List<Integer> postorderTraversal2(TreeNode root) {
List<Integer> res=new ArrayList<>();
Stack<TreeNode> nodeStack=new Stack<>();
Stack<Integer> nodeState=new Stack<>();//记录右节点是否已经訪问过,1表示已经訪问了,0表示未訪问
if(root==null)
return res;
else {
nodeStack.push(root);
nodeState.push(0);
root=root.left;
}
while(!nodeStack.isEmpty())
{
while(root!=null)
{
nodeStack.push(root);
nodeState.push(0);
root=root.left;
}//当这个循环跳出的时候 说明nodeStak栈顶的那个节点没有左节点
if(nodeState.peek()==1)//假设这时候已经訪问过右节点了 这时候就能够訪问根节点
{
res.add(nodeStack.pop().val);
nodeState.pop();//把根节点相应的状态值去除
}
else {//訪问右节点
root=nodeStack.peek().right;
nodeState.pop();//更改状态值 由0变1
nodeState.push(1);
}
}
return res;
}
LeetCode145 Binary Tree Postorder Traversal Java题解(递归 迭代)的更多相关文章
- leetcode 145. Binary Tree Postorder Traversal ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- [LeetCode145]Binary Tree Postorder Traversal
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- Binary Tree Postorder Traversal(各种非递归实现,完美利用栈结构模拟)
1.后序遍历的非递归实现.(左右根) 难点:后序遍历的非递归实现是三种遍历方式中最难的一种.因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来 ...
- Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- 二叉树前序、中序、后序非递归遍历 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: 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 ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
随机推荐
- Google Chrome浏览器调试
作为Web开发人员,我为什么喜欢Google Chrome浏览器 [原文地址:http://www.cnblogs.com/QLeelulu/archive/2011/08/28/2156402.ht ...
- JFinal项目eclipse出现Unknown column 'createtime' in 'order clause' 的错误
JFinal项目eclipse出现Unknown column 'createtime' in 'order clause' 的错误,在本次项目中的原因是我的表中的字段信息中创建时间的字段是creat ...
- IOS 3D UI --- CALayer的transform扩展
http://www.cocoachina.com/bbs/read.php?tid=117061 例子代码可以在 http://download.csdn.net/detail/worldmatri ...
- jsMap地图网点
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name=& ...
- $.fn.extend的用法
$.fn.extend({ sfMessages: function(m) { $("p.tips").html('<span class="circle ioc ...
- log4j动态文件名
在项目中,对log的输出有多种多样的要求,下面具体分析一下动态log文件名输出的. 一,按照用户ID来生成log,这种情况,可以根据每个用户ID来动态生成logger. 代码如下: import or ...
- Rightmost Digit (求n^n最后一位)
Description Given a positive integer N, you should output the most right digit of N^N. Input The ...
- 九度oj 题目1473:二进制数(stack)
题目1473:二进制数 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:9371 解决:2631 题目描述: 大家都知道,数据在计算机里中存储是以二进制的形式存储的. 有一天,小明学了C语 ...
- 在ubuntu 18.04下装有线守护wg
wg你懂的是什么,下面讲讲我这段时间安装过程,发觉这个软件和CENTOS 7下面不是很合拍,它更适合ubuntu,因为ubuntu仍然用iptables,而CENTOS 早就用了firewalld了, ...
- [JLOI2008]提示问题
题目描述 最近在JLOI网上的一个流行游戏中,选手要回答很难的问题.假如在规定时间内不能回答,系统将给出1个提示,之后再依次给出第2,3个提示.出现在答案中的是字母和下列字符: '.',',',':' ...