lintcode:二叉树的中序遍历
题目:
二叉树的中序遍历
给出一棵二叉树,返回其中序遍历
给出二叉树 {1,#,2,3}
,
1
\
2
/
3
返回 [1,3,2]
.
你能使用非递归算法来实现么?
解题:
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: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
// write your code here
ArrayList<TreeNode> p = new ArrayList<TreeNode>();
ArrayList<Integer> res = new ArrayList<Integer>();
while(root != null || p.size() != 0){
while(root != null){
p.add(root);
root = root.left;
}
root = p.get(p.size()-1);
p.remove(p.size()-1);
res.add(root.val);
root = root.right;
}
return res;
} }
总耗时: 1238 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: Inorder in ArrayList which contains node values.
"""
def inorderTraversal(self, root):
# write your code here
p = [root]
res = [0]
while root is not None or len(p) != 1:
while root is not None:
p.append(root)
root = root.left
root = p[len(p)-1]
del p[len(p)-1]
res.append(root.val)
root = root.right
n = len(res)
return res[1:n]
总耗时: 263 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: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
// write your code here ArrayList<Integer> res = new ArrayList<Integer>();
res = inorderTrun(res,root);
return res;
}
public ArrayList<Integer> inorderTrun(ArrayList<Integer> res,TreeNode root){
if(root == null)
return res;
if(root!=null){
if(root.left!=null){
res = inorderTrun(res,root.left);
}
res.add(root.val);
if(root.right!=null){
res = inorderTrun(res,root.right);
}
}
return res;
} }
总耗时: 1714 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: Inorder in ArrayList which contains node values.
"""
def inorderTraversal(self, root):
# write your code here
res = []
res = self.inorderTrun(res,root)
return res def inorderTrun(self,res,root):
if root==None:
return res
if root.left!=None:
res = self.inorderTrun(res,root.left)
res.append(root.val)
if root.right!=None:
res = self.inorderTrun(res,root.right)
return res
总耗时: 213 ms
根据上面的程序理解,可根据栈实现,上面定义的ArrayList也是起到栈的作用
lintcode:二叉树的中序遍历的更多相关文章
- [LintCode] 二叉树的中序遍历
The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 数据结构《10》----二叉树 Morris 中序遍历
无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...
- LeetCode(94):二叉树的中序遍历
Medium! 题目描述: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗 ...
- 【LeetCode题解】94_二叉树的中序遍历
目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...
- LintCode-67.二叉树的中序遍历
二叉树的中序遍历 给出一棵二叉树,返回其中序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,3,2]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code /** ...
- LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal
题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- Leetcode题目94.二叉树的中序遍历(中等)
题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...
随机推荐
- NSS_04 extjs中grid接收datetime类型参数列
今天在做用户列表时发现, asp.net mvc3的控制器在返回JsonResult结果时, 会把对象内的DateTime类型成员,解析为类似于\/Date(1238606590509)\/的格式 , ...
- 重拾C,一天一点点
数据类型及长度 char 字符型,占用一个字节 int 整型,通常代表特定机器中整数的自然长度 short 16位 int 16位或32位 ...
- 利用Unicode属性移除文本中的标点符号
原文:http://bbs.csdn.net/topics/270033191 摘抄: str = str.replaceAll("[\\pP‘’“”]", "&qu ...
- Highcharts条形与柱形同时显示
var chart; $(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'chart_combo' //关联页面元素di ...
- Python数据结构——栈、队列的实现(一)
1. 栈 栈(Stack)是限制插入和删除操作只能在一个位置进行的表,该位置是表的末端,称为栈的顶(top).栈的基本操作有PUSH(入栈)和POP(出栈).栈又被称为LIFO(后入先出)表. 1.1 ...
- springMVC之事务配置(问题来源:为什么数据保存不了)
参考文章:http://www.cnblogs.com/leiOOlei/p/3725911.html 自己的亲身体会,来源问题this.sessionFactory.getCurrentSessio ...
- 绘制dot 图
常用参数 格式:dot -T<type> -o<outfile> <infile.dot> 输入文件是<infile.dot>,生成的格式由<ty ...
- (转)c指针
转自:http://www.cnblogs.com/wchhuangya/archive/2009/12/24/1631121.html 这两天开始搞BREW了,用的是C的语法.上学时学过的C都还给学 ...
- Get current time and date on Android
You could use: Calendar c =Calendar.getInstance();int seconds = c.get(Calendar.SECOND); There are pl ...
- iPhone OS 开发 - 了解并解决代码签名问题
译者:Jestery 发表时间:2010-04-24浏览量:21082评论数:0挑错数:0 了解并解决代码签名问题 (为保持跟开发环境以及APPLE开发者社区网站结构对应,一些名词未作翻译) 绝大多数 ...