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 ...
随机推荐
- jQuery对input中radio的一些操作
通过jQuery获取页面中的所有radio对象,遍历页面中的radio,取消选中的标签,因为使用到jQuery时间,因此引用到了网上公共的js,这只是本人的一些总结,大神勿喷. <html> ...
- linux查看硬件信息
1,查看CPU信息:cat /proc/cpuinfo2,查看板卡信息:cat /proc/pci3,查看USB设备:cat /proc/bus/usb/devices4,查看PCI信息:lspci ...
- web前端--知识点,笔记叠加(javascript,jquery,html5+css3.0,ajax)
函数传参列表,获取方法arguments的使用 function arg(){ var str = '总共传了'+arguments.length+'个参数\n'; for(var i=0;i< ...
- 本地安装gem install --local redis-stat-0.4.13.gem
因为主机环境不能联外网,悲哀,所以只能想办法下载包,上传到主机来安装 环境:el6.x86_64 1. gem 安装[http://centos.ustc.edu.cn/centos/6/os/x86 ...
- PowerPoint Office Mix 插件
一个内嵌在PowerPoint里的一个教学工具,可以以PPT为核心录制视频. 点下面链接了解并安装 https://mix.office.com/ 首先这货是免费,当然是基于PowerPoint的基础 ...
- java 泛型通配符 extends, super
引自:http://sharewind.iteye.com/blog/1622164 关键字说明 ? 通配符类型 <? extends T> 表示类型的上界,表示参数化类型的可能是T 或是 ...
- Windows下将txt导入MySQL及远程连接设置
1.修改字符编码,全部修改为gbk.这样修改,重启后又会恢复默认值. show variables like '%char%'; set character_set_database=gbk; 其中, ...
- textview点击后selector的pressed无效果
原因: 需要配置 android:clickable="true" 这个跟开发环境有关,我之前用的android studio 就不需要这一项,默认可以点击. ********* ...
- 高德开发 android 出现 key 鉴权失败
环境windows + android studio 原因: 曾经更改过key.store 解决办法: 首先运行cmd移动到keystore的目录下keytool -list -keystore 文件 ...
- SQL Server数据库事务日志序列号(LSN)介绍
原文:http://blog.csdn.net/tjvictor/article/details/5251463 日志序列编号(LSN)是事务日志里面每条记录的编号. 当你执行一次备份时,一些 ...