题目:

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题解(递归 迭代)的更多相关文章

  1. leetcode 145. Binary Tree Postorder Traversal ----- java

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  2. [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 ...

  3. Binary Tree Postorder Traversal(各种非递归实现,完美利用栈结构模拟)

    1.后序遍历的非递归实现.(左右根) 难点:后序遍历的非递归实现是三种遍历方式中最难的一种.因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来 ...

  4. Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历

    给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...

  5. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  6. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  7. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  8. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  9. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

随机推荐

  1. Google Chrome浏览器调试

    作为Web开发人员,我为什么喜欢Google Chrome浏览器 [原文地址:http://www.cnblogs.com/QLeelulu/archive/2011/08/28/2156402.ht ...

  2. echo追加字符串到文件末尾

    1.覆盖    echo  "string" >  filename 2.追加   echo "string"  >>  filename

  3. CSU 2018年12月月赛 H(2220): Godsend

    Description Leha somehow found an array consisting of n integers. Looking at it, he came up with a t ...

  4. Error opening session. Cause: java.lang.NullPointerExcept.

    在学mybatis时遇到这个问题,后面发现时打错了一个字母,发现后分享出来,如果发现这个错误也能够更好的排除错误. 如图可以发现我不小心把default打成了defaule所以出现了这个错误,也找了好 ...

  5. 【笔记】搭建OpenWrt编译环境

    参考书目<B智能路由开发指南> 目标:搭建一个OpenWrt编译环境,可以同时在家里和公司使用. [2018-09-13] 刚开始想用自己的电脑共享远程桌面,但不知道什么原因搞不定,所以干 ...

  6. IDEA基本使用及配置(1)

    前言:现在IDEA用的人很多,我以前都是用Eclipse的,都说这个IDE比较智能.好用,于是学习一下. IDEA与Eclipse目录结构对比: IDEA中的Project相当于Eclispe中的wo ...

  7. PHP实现定时任务的几种方式

    关于定时任务,之前以前认识了一种最常用的:crontab定时任务.通过linux的定时任务去实现.今天又认识了一下php实现定时方式的其它方式,总结一下. 一 服务器定时任务 服务器定时任务,其实就是 ...

  8. Fiddler抓取https相关设置

    转自:https://www.cnblogs.com/joshua317/p/8670923.html 很多使用fiddler抓包,对于http来说不需太多纠结,随便设置下就能用,但是抓取https就 ...

  9. Java反射机制(Reflect)解析-----https://www.cnblogs.com/fzz9/p/7738381.html

    Java反射机制(Reflect)解析-----https://www.cnblogs.com/fzz9/p/7738381.html

  10. AndroidSweetSheet:ViewPager的实现(2)

     AndroidSweetSheet:ViewPager的实现(2) 附录文章9说明了AndroidSweetSheet典型的列表样式实现,本文写一个例子,说明AndroidSweetSheet以 ...