题目:

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. JavaSE-16 集合框架

    学习要点 Java集合框架内容 ArrayList和LinkedList HashMap Iterator 泛型集合 Java的集合框架 1  概述 数据结构是以某种形式将数据组织在一起的集合,它不仅 ...

  2. redis简介以及安装

    redis作为开源的高性能的键值对数据库,本身是单线程的,性能虽然没有memcache高,但是也是性能跟memcache相差无几的,memcache是多线程的,但是redis本身功能更加强大,学习一下 ...

  3. github some rank

    github some rank http://githubrank.com/

  4. reactNative 打包那些事儿

    我们项目测试时一般是debug版本,打包上线,一般是release版本,所以在测试和打包时会走不同的方法,如上图所示. 在debug版本中,会走我们本地服务器,也就是自己电脑上的服务.在release ...

  5. STM32F407 GPIO原理 个人笔记

    datasheet(STM32F407ZGT6.pdf)中,IO structure 为FT,表示容忍5V电压 后面的uart1_TX之类,表示端口复用 共有A~G7组IO口, 每组16个IO口:0~ ...

  6. Leetcode 143.重排链表

    重排链表 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示 ...

  7. 九度oj 题目1516:调整数组顺序使奇数位于偶数前面

    题目1516:调整数组顺序使奇数位于偶数前面 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:3416 解决:1091 题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序, ...

  8. 【ZJOI2017 Round1后记】

    2017.4.1: NOIP+Round1综合成绩出来,标准分离续命线差了80分,果然还是联赛坑挖太大了…… 不管怎么说能续命的话还是要试一下的…… 发毒誓:Round2前不打手游,不看NGA,不看星 ...

  9. Max Num

    Problem Description There are some students in a class, Can you help teacher find the highest studen ...

  10. Gym100812 L 扩展欧几里得

    L. Knights without Fear and Reproach time limit per test 2.0 s memory limit per test 256 MB input st ...