题目:

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. A10. JVM 对象

    [概述] 首先需要了解对象在内存中的存储布局,其次需要了解对对象的访问定位. [对象的内存布局] 在 HotSpot 虚拟机中,对象在内存中存储的布局可以分为 3 块区域:对象头(Header).实例 ...

  2. torch学习笔记(二) nn类结构-Linear

    Linear 是module的子类,是参数化module的一种,与其名称一样,表示着一种线性变换. 创建 parent 的init函数 Linear的创建需要两个参数,inputSize 和 outp ...

  3. PAT 1073. 多选题常见计分法

    PAT 1073. 多选题常见计分法 批改多选题是比较麻烦的事情,有很多不同的计分方法.有一种最常见的计分方法是:如果考生选择了部分正确选项,并且没有选择任何错误选项,则得到50%分数:如果考生选择了 ...

  4. java的动态代理机制详解-----https://www.cnblogs.com/xiaoluo501395377/p/3383130.html

    java的动态代理机制详解-----https://www.cnblogs.com/xiaoluo501395377/p/3383130.html

  5. CodeForcesGym 100517B Bubble Sort

    Bubble Sort Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...

  6. xtu summer individual-4 A - Beautiful IP Addresses

    Beautiful IP Addresses Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on Code ...

  7. 实现下载pdf文件

    //打开文件//$fileDir为文件路径 $fileName为文件名称$file = fopen($fileDir . DS . $fileName, "r");//输入文件标签 ...

  8. POJ-1679 The Unique MST,次小生成树模板题

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K       Description Given a connected undirec ...

  9. amoeba连接mysql--ERROR 2006 (HY000): MySQL server has gone away

    amoeba下载地址:http://sourceforge.net/projects/amoeba/files amoeba version:amoeba-mysql-binary-2.1.0-RC5 ...

  10. wangEditor

    wangEditor 基于javascript和css开发的 Web富文本编辑器, 轻量.简洁.易用.开源免费 http://www.wangeditor.com/index.html API htt ...