本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42876769

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?

思路:

(1)题意为后序遍历二叉树。遍历顺序为左—>右—>根。

(2)考虑到用递归比较简单,本文使用递归的思想进行解决,由于比较简单这里不累赘,详见下方代码。

(3)希望本文对你有所帮助。

算法代码实现如下:

/**
 * @author liqq
 */
public List<Integer> PostorderTraversal(TreeNode root) {
	List<Integer> result = new LinkedList<Integer>();
	if (root != null) {
		Post_order(result, root.left);
		Post_order(result, root.right);
		result.add(root.val);
	}
	return result;
}

private void Post_order(List<Integer> result, TreeNode curr) {
	if (curr != null) {
		Post_order(result, curr.left);
		Post_order(result, curr.right);
		result.add(curr.val);
	}
}

Leetcode_145_Binary Tree Postorder Traversal的更多相关文章

  1. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  2. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  3. [LeetCode] N-ary Tree Postorder Traversal N叉树的后序遍历

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

  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 | 二叉树后序遍历 | Hard

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

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

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

  7. Leetcode590. N-ary Tree Postorder Traversal

      590. N-ary Tree Postorder Traversal 自己没写出来   优秀代码: """ # Definition for a Node. cla ...

  8. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

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

  9. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. 整理的Java List Set Map是否有序,元素是否允许重复

    整理的Java List Set Map是否有序,元素是否允许重复的说明,如下图:

  2. MySQL备忘录

    1 数据库概念(了解) 1.1 什么是数据库 数据库就是用来存储和管理数据的仓库! 数据库存储数据的优先: l 可存储大量数据: l 方便检索: l 保持数据的一致性.完整性: l 安全,可共享: l ...

  3. 有没有最好的学习Angularjs2的视频入门体验?

    Which are the best video tutorials for learning AngularJS 2? 有没有最好的学习Angularjs2的视频入门体验? 翻译来源:https:/ ...

  4. Scala:访问修饰符、运算符和循环

    http://blog.csdn.net/pipisorry/article/details/52902234 Scala 访问修饰符 Scala 访问修饰符基本和Java的一样,分别有:privat ...

  5. TV Metro界面(仿泰捷视频TV版)源码解析

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52822499 前言:上一篇介绍了 ...

  6. Unity角色残影特效

    残影特效在网上有很多例子,比如这个,我参考着自己整合了一下,算是整合了一个比较完整且特别简单易用的出来,只需要一个脚本挂上去无需任何设定就能用. 这里只针对SkinnedMeshRenderer的网格 ...

  7. Hibernate之实体关系映射

    延迟加载与即时加载 例如Person类和Email类是一对多关系,如果设为即时加载,当加载Person时,会自动加载Email,如果设置为延迟加载,当第一次调用person.getEmails()时才 ...

  8. 驱动中如何给ring3层应用程序提权

    为什么会有这个需求就不用我多说了吧:) 目前在驱动中提权我知道的有三种办法 1. 该方法来源于stoned bootkit,主要原理是把services.exe的EPROCESS中的Token值取出来 ...

  9. JQuery 初探

    放暑假了,终于有时间能学点前端的东西了.JQuery就是我第一个选择,锋利的JQuery.这本书真的很好.下面以一个ToggleButton形式的小例子开场吧. 引入JQuery库 在网页上引用JQu ...

  10. java之IO流详解(二)

    好了,昨天讲了字节流,现在我们就来讲字符流吧... 字符流可以这样理解,字符流 = 字节流 + 编码表,目的是为了更好的操作中文字符(注:字符流只可以可以操作字符类型的文件,不能操作影音图像文件,要操 ...