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 ...
随机推荐
- C# html table转excel
1. protected void ebtDC_Click(object sender, EventArgs e) { string elxStr = "<table><t ...
- left_v2.js
$(document).ready(function(){ $(".mc_left a").each(function(){ var href = $(this).attr(&qu ...
- vue全选与反选以及通过使用如何filter删除数据
在vue学习经常遇到的一些基本问题,下面是购物车里面的部分功能,分享给初学者,直接上源码: <!DOCTYPE html><html> <head> <met ...
- BZOJ3545 Peaks 离线处理+线段树合并
题意: 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只经 ...
- python中的句柄操作
python中的句柄操作 制作人:全心全意 通过窗口标题获取句柄 import win32gui hld = win32gui.FindWindow(None,u"Adobe Acrobat ...
- PHP:Mysql判断KEY是否存在 如果存在走修改 如果不存在走添加
文章来源:http://www.cnblogs.com/hello-tl/p/7738113.html 0.PHP代码 <?php /** * POST 传参 * * 例子 添加修改 使用同一个 ...
- MySQL-----唯一索引
唯一索引: 单列唯一索引和联合唯一索引 索引是为了加速查找. 唯一索引是加了约束条件.例如主外键. 唯一索引的约束: 约束不能重复(可以为空) 主键不能重复(不能为空) 加速查找 create tab ...
- python书籍推荐:Python数据科学手册
所属网站分类: 资源下载 > python电子书 作者:today 链接:http://www.pythonheidong.com/blog/article/448/ 来源:python黑洞网 ...
- python flask获取微信用户信息报404,nginx问题
在学习flask与微信公众号时问题,发现测试自动回复/wechat8008时正常,而测试获取微信用户信息/wechat8008/index时出现404.查询资料后收发是nginx配置问题. 在loca ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...