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].

Note: Recursive solution is trivial, could you do it iteratively?

中文:二叉树的兴许遍历(左-右-根)。能用非递归吗?

递归:

public class BinaryTreePostorderTraversal {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if(root == null)
return list;
list.addAll(postorderTraversal(root.left));
list.addAll(postorderTraversal(root.right));
list.add(root.val);
return list;
}
// Definition for binary tree
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
}

非递归:

    public List<Integer> postorderTraversal(TreeNode root){
List<Integer> list = new ArrayList<Integer>();
if(root == null)
return list;
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);//最后訪问
while(!stack.isEmpty()){
TreeNode current = stack.peek();
//根节点无子节点
if(current.left == null && current.right == null){
list.add(current.val);
stack.pop();
}
if(current.left != null){
stack.push(current.left);
current.left = null;
continue;
}
if(current.right != null){
stack.push(current.right);
current.right = null;
continue;
}
}
return list;
}

LeetCode——Binary Tree Postorder Traversal的更多相关文章

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

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

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

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

  3. Leetcode Binary Tree Postorder Traversal

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

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

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

  5. [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索

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

  6. LeetCode: Binary Tree Postorder Traversal [145]

    [题目] Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi ...

  7. LeetCode Binary Tree Postorder Traversal(数据结构)

    题意: 用迭代法输出一棵二叉树的后序遍历结果. 思路: (1)用两个栈,一个存指针,一个存标记,表示该指针当前已经访问过哪些孩子了. /** * Definition for a binary tre ...

  8. leetcode Binary Tree Postorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  9. leetcode Binary Tree Postorder Traversal 二叉树后续遍历

    先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...

随机推荐

  1. Android Material Design系列之主题样式介绍说明

    今天这篇文章应该算是Material Design系列的补充篇,因为这篇文章本来应该放到前面讲的,因为讲的是主题嘛,对于一些状态和颜色的介绍,因为我们一新建一个项目时,系统自带了三个属性的颜色,现在就 ...

  2. VS2010 MFC中 创建文件夹及文件判空的方法

    1. MFC中 创建文件夹的方法如下: CString strFolderPath = "./Output"; //判断路径是否存在 if(!PathIsDirectory(str ...

  3. 版本控制工具:SVN和Maven的区别

    一.只有svn的情况 首先考虑没有maven的情况.这样的话,项目组每个开发人员,都需要在本地check out所有的源码. 每次提交之前,需要先更新周边工程的代码.由于工程之间是依赖的,所以很可能需 ...

  4. Swift 函数的定义与调用(Defining and Calling Functions)

    当你定义一个函数时,你能够定义一个或多个有名字和类型的值.作为函数的输入(称为參数.parameters).也能够定义某种类型的值作为函数运行结束的输出(称为返回类型). 每一个函数有个函数名,用来描 ...

  5. .NET中XML 注释 SandCastle 帮助文件.hhp 使用HTML Help Workshop生成CHM文件

    一.摘要 在本系列的第一篇文章介绍了.NET中XML注释的用途, 本篇文章将讲解如何使用XML注释生成与MSDN一样的帮助文件.主要介绍NDoc的继承者:SandCastle. .SandCastle ...

  6. Mongodb性能调优

    摘要 1. Mongodb 适用场景简介 2. Mongodb 性能监控与分析 3. Mongodb 性能优化建议 关于Mongodb的几个大事件 1.根据美国数据库知识大全官网发布的DB热度排行,M ...

  7. make -j 4 echo !$

    make -j 4 #以cpu四核编译 !$上一次命令空格后的部分

  8. Python将JSON格式数据转换为SQL语句以便导入MySQL数据库

    前文中我们把网络爬虫爬取的数据保存为JSON格式,但为了能够更方便地处理数据.我们希望把这些数据导入到MySQL数据库中.phpMyadmin能够把MySQL数据库中的数据导出为JSON格式文件,但却 ...

  9. linux 木马

    参考 http://wangzan18.blog.51cto.com/8021085/1740113 http://www.cnblogs.com/jluzhsai/p/3756280.html  ( ...

  10. 【Excle数据透视表】如何复制数据透视表

    左边创建完数据透视表,右边是复制过去的部分数据透视表---显示数值状态的内容,为什么复制过来的不是数据透视表呢? 解决办法: 全选定数据透视表再进行粘贴复制 步骤一 单击数据透视表任意单元格→分析→操 ...