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. C# 中的结构类型(struct type)

    ylbtech- .NET-Basic:C# 中的结构类型(struct type) C# 中的结构类型(struct type) 1.A,相关概念返回顶部   像类一样,结构(struct)是能够包 ...

  2. SSD配置

    SSD: Single Shot MultiBox Detector - 运行“ make -j32”时出错: nvcc warning : The 'compute_20', 'sm_20', an ...

  3. DFRobot万物互联大赛第二轮

    前言 最近放在阳台的花草被啥东西给吃了,然后厨房挂在墙上的小虾米也不知道咋的被抓破吃光了(我怀疑是隔隔壁两条泰迪),所以打算做个简单的项目,教训一下偷吃贼.时间比较仓促,内容比较多,能力有比较有限,好 ...

  4. css:选择器

    http://blog.csdn.net/xyz121323693/article/details/8516297 交集选择器 并集选择器 后代选择器 子代选择器 http://www.cnblogs ...

  5. Ruby之Rspec的报错解决

    #enconding:utf-8 require 'selenium-webdriver' require 'rspec' describe "baidu main page" d ...

  6. php excel文件导出之二 图像导出

    PHP文件导出 之图像 和 文字同一时候导出 事实上之前写了个php文件导出.跟这个极为相似,由于项目须要对图像进行导出.查询一番.又写了一个, 这个能实现图像的导出(仅仅能是本地图像,不能使用远程图 ...

  7. 面试题:使用finalkeyword修饰一个变量时,是引用不能变,还是引用的对象不能变?

    /* * 问题:使用finalkeyword修饰一个变量时,是引用不能变,还是引用的对象不能变 * 答: * 使用finalkeyword修饰一个变量时,是指引用变量不能变,引用变量所指向的对象中的内 ...

  8. 好用的公共 DNS

    Google Public DNS: 8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844 OpenDNS: 208.67.222.222 ...

  9. HttpRuntime Cache用法及参数解释

    自己用到的: HttpRuntime.Cache.Insert("SchoolBindKcChangci", SchoolBindKcChangci, null, DateTime ...

  10. Ruby on Rails 路由解析

    为了更好的阅读体验.欢迎訪问 作者博客原文 Route是什么 Rails中URL的约定严格基于RESTful风格的.client的请求事实上是在操作一些资源.同一资源的不同的请求动作(GET, POS ...