Given a binary tree, return the postorder traversal of its nodes' values.

For example:
Given binary tree [1,null,2,3],

   1
\
2
/
3

return [3,2,1].

递归:

 class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> postorderTraversal(TreeNode root) {
help(root);
return res;
}
private void help(TreeNode root){
if(root == null) return ;
help(root.left);
help(root.right);
res.add(root.val);
}
}

非递归:

终极版

 class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
List<Integer> res = new ArrayList<Integer>();
while(root!=null||!s.isEmpty()){
while(root!=null){
s.push(root);
res.add(0,root.val);
root = root.right;
} if(!s.isEmpty()){
root = s.pop();
root = root.left;
}
}
return res;
}
}

跟前序遍历差不多 ,stack 保存左子树,向右遍历,反向保存res.

 class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> postorderTraversal(TreeNode root) {
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode cur = root;
while(cur!=null || !stack.isEmpty()){
while(cur!=null){
res.add(0,cur.val);
stack.push(cur.left);
cur = cur.right;
}
cur = stack.pop();
}
return res;
}
}

145. Binary Tree Postorder Traversal(二叉树后序遍历)的更多相关文章

  1. [Leetcode] Binary tree postorder traversal二叉树后序遍历

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

  2. LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard

    题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...

  3. LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)

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

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

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

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

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

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

    题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...

  7. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

  8. 145 Binary Tree Postorder Traversal 二叉树的后序遍历

    给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...

  9. 145.Binary Tree Postorder Traversal---二叉树后序非递归遍历

    题目链接 题目大意:后序遍历二叉树. 法一:普通递归,只是这里需要传入一个list来存储遍历结果.代码如下(耗时1ms): public List<Integer> postorderTr ...

  10. [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

随机推荐

  1. 基于docker部署的微服务架构(四): 配置中心

    原文:http://www.jianshu.com/p/b17d65934b58%20 前言 在微服务架构中,由于服务数量众多,如果使用传统的配置文件管理方式,配置文件分散在各个项目中,不易于集中管理 ...

  2. Mysql命令行添加用户并且给予远程访问服务器的权限

    --查询用户SELECT User, Password, Host FROM user; --创建一个用户,任意主机可以登录%,密码是123456 '; -- 给用户赋予所有权限 GRANT ALL ...

  3. sqlalchemy 判断字段是否存在

    1.low方法: (1)person_obj=session.query(Person).filter(Person.name=='jack') print (person_obj.count()) ...

  4. 三角剖分算法(delaunay)

    开篇 在做一个Low Poly的课题,而这种低多边形的成像效果在现在设计中越来越被喜欢,其中的低多边形都是由三角形组成的. 而如何自动生成这些看起来很特殊的三角形,就是本章要讨论的内容. 项目地址:  ...

  5. 用原生Canvas写贪吃蛇及问题解决

    为了学习Canvas,写了这个小游戏贪吃蛇供自己和大家学习 Github: https://github.com/zhiyishou/Gsnake Play On: http://zhiyishou. ...

  6. iOS开发之--从URL加载图片

    + (UIImage *) imageFromURLString: (NSString *) urlstring { // This call is synchronous and blocking ...

  7. iOS开发之 -- bundle程序束的制造

    我们在写项目的时候,需要添加大量的图片,这个时候除了在x-code-->Assets文件里面添加图片外,还可以添加程序束,这样的话 项目看起来比较整齐,也显得比较专业,下面就来说一下程序束的制造 ...

  8. 在Ubuntu上搭建hive环境

    一.准备软件 二.安装虚拟机 1.新建虚拟机向导 2.安装客户机操作系统 3.用户名密码设置 4.设置虚拟机名称和保存位置 5.处理器设置 6.设置虚拟机内存 7.然后一直next下去(有的根据自己的 ...

  9. Sublime Text 3如何编译运行c++程序

    扯 去了一趟清北学堂感觉自己玩的特别嗨,算法没学到什么,前端和爬虫的知识到是会了不少. 然后知道了有一个叫做sublime text 3的编辑器,好用不好用不知道,就冲着它好看,就决定以后就用它了. ...

  10. Exchange Database Status(Copy Status ,Content Index State,QueueLength,Move Status...)

    Copy Status Description Mounted The active copy is online and accepting client connections. Only the ...