lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目:
二叉树的后序遍历
给出一棵二叉树,返回其节点值的后序遍历。
给出一棵二叉树 {1,#,2,3}
,
1
\
2
/
3
返回 [3,2,1]
你能使用非递归实现么?
解题:
递归程序好简单
Java程序:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in ArrayList which contains node values.
*/
public ArrayList<Integer> postorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> res = new ArrayList<Integer>();
res = postorder(res,root);
return res;
}
public ArrayList<Integer> postorder(ArrayList<Integer> res,TreeNode root){
if(root==null)
return res;
if(root.left!=null)
res = postorder(res,root.left);
if(root.right!=null)
res = postorder(res,root.right);
res.add(root.val);
return res;
}
}
总耗时: 1210 ms
Python程序:
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
""" class Solution:
"""
@param root: The root of binary tree.
@return: Postorder in ArrayList which contains node values.
"""
def postorderTraversal(self, root):
# write your code here
res = []
res = self.postorder(res,root)
return res def postorder(self,res,root):
if root==None:
return res
if root.left!=None:
res = self.postorder(res,root.left)
if root.right!=None:
res = self.postorder(res,root.right)
res.append(root.val)
return res
总耗时: 380 ms
非递归程序,直接来源
Java程序:
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in ArrayList which contains node values.
*/
public ArrayList<Integer> postorderTraversal(TreeNode root) {
// write your code here
int a = 1;
ArrayList<TreeNode> s = new ArrayList<TreeNode>();
ArrayList<Integer> res = new ArrayList<Integer>();
if (root == null) return res;
while(a == 1){
while(root.left != null || root.right != null){
if (root.left != null){
s.add(root);
root = root.left;
}
else{
s.add(root);
root = root.right;
}
}
TreeNode y = s.get(s.size()-1);
while (root == y.right || y.right == null){
res.add(root.val);
s.remove(s.size()-1);
if (s.size() == 0){
a = 0;
res.add(y.val);
break;
}
root = y;
y = s.get(s.size()-1);
}
if (root == y.left && y.right != null){
res.add(root.val);
root = y.right;
}
}
return res;
}
}
总耗时: 1388 ms
Python程序:
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
""" class Solution:
"""
@param root: The root of binary tree.
@return: Postorder in ArrayList which contains node values.
"""
def postorderTraversal(self, root):
# write your code here
a = 1
s = [root]
res = []
if root is None:
return res[1:1]
while a == 1:
while root.left is not None or root.right is not None:
if root.left is not None:
s.append(root)
root = root.left
else:
s.append(root)
root = root.right
y = s[len(s)-1]
while root == y.right or y.right is None:
res.append(root.val)
del s[len(s)-1]
if len(s) == 1:
a = 0
res.append(y.val)
break
root = y
y = s[len(s)-1]
if root == y.left and y.right is not None:
res.append(root.val)
root = y.right
return res
总耗时: 360 ms
lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历的更多相关文章
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
- 145 Binary Tree Postorder Traversal 二叉树的后序遍历
给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3], 1 \ 2 / 3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...
- Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...
随机推荐
- NSS_05 数据访问选型
在数据访问层上很想用orm框架, 选用Nhibernate或ef, 可以直接操作类对象, 避免转换, 更加的面向对象,更重要的是开发起来就方便多了. 但是从网上了解到这些框架太高级了, 用得不好到时会 ...
- 卸载CentOS 5.4自带的OpenJDK,配置新的Java环境
本文CentOS版本为5.4 final,使用图形界面与命令结合的操作方式,由于CentOS 5.4在默认情况下,会安装OpenOffice之类的软件,而这些软件需要Java支持,因此系统会默认安装一 ...
- openerp经典收藏 字段定义详解(转载)
字段定义详解 原文地址:http://shine-it.net/index.php/topic,2159.0.htmlhttp://blog.sina.com.cn/s/blog_57ded94e01 ...
- UNIX环境高级编程-环境配置
环境配置步骤如下. 1. 下载源文件:http://www.apuebook.com/src.tar.gz. 2. 复制src.tar.gz文件到/home/me/mydir/unixl/目录(自 ...
- Ubuntu14.04下中山大学锐捷上网设置
Ubuntu14.04下中山大学锐捷上网设置 打开终端后的初始目录是 -,Ubuntu安装完毕默认路径,不是的请自行先运行cd ~ 非斜体字命令行方法,斜体字是图形管理方法,二选一即可 记得善用Tab ...
- C++中用辗转相除法求两个数的最大公约数和最小公倍数
两个数的最大公约数:不能大于两个数中的最小值,算法口诀:小的给大的,余数给小的,整除返回小的,即最大公约数,(res=max%min)==0? max=min,min=res return min; ...
- VBS基础篇 - 循环
经常地,当编写代码时,我们希望将一段代码执行若干次,我们可以在代码中使用循环语句来完成这项工作. 循环可分为三类:一类在条件变为 False 之前重复执行语句,一类在条件变为 True 之前重复执行语 ...
- extern "C"——用“C”来规约在C++中用C的方式进行编译和链接
C++中的extern “C”用法详解 extern "C"表明了一种编译规约,其中extern是关键字属性,“C”表征了编译器链接规范.对于extern "C& ...
- 例题-Quota 实作:
假设这五个用户均需要进行磁盘配额限制,每个用户的配额为 2GB (hard) 以及 1.8GB (soft),该如何处理? 答: 这一题实作比较难,因为必须要包括文件系统的支持.quota 数据文件建 ...
- try-catch-finally中return的执行情况分析
try-catch-finally中return的执行情况分析: 1.在try中没有异常的情况下try.catch.finally的执行顺序 try --- finally 2.如果try中有异常,执 ...