题目:

二叉树的中序遍历

给出一棵二叉树,返回其中序遍历

样例

给出二叉树 {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:二叉树的中序遍历的更多相关文章

  1. [LintCode] 二叉树的中序遍历

    The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and  ...

  2. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  3. 数据结构《10》----二叉树 Morris 中序遍历

    无论是二叉树的中序遍历还是用 stack 模拟递归, 都需要 O(n)的空间复杂度. Morris 遍历是一种 常数空间 的遍历方法,其本质是 线索二叉树(Threaded Binary Tree), ...

  4. LeetCode(94):二叉树的中序遍历

    Medium! 题目描述: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗 ...

  5. 【LeetCode题解】94_二叉树的中序遍历

    目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...

  6. LintCode-67.二叉树的中序遍历

    二叉树的中序遍历 给出一棵二叉树,返回其中序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 返回 [1,3,2]. 挑战 你能使用非递归实现么? 标签 递归 二叉树 二叉树遍历 code /** ...

  7. LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal

    题目: 给定一个二叉树,返回它的中序 遍历. Given a binary tree, return the inorder traversal of its nodes' values. 示例: 输 ...

  8. LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)

    94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...

  9. Leetcode题目94.二叉树的中序遍历(中等)

    题目描述: 给定一个二叉树,返回它的中序遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 思路解析: 1 ...

随机推荐

  1. Lucene Field

    org.apache.lucene.demo.IndexFiles类中,使用递归的方式去索引文件.在构造了一个IndexWriter索引器之后,就可以向索引器中添加Doucument了,执行真正地建立 ...

  2. 读取XML

    public sealed class ConfigManger { public XDocument XmlDocs { set; get; } string path = @"{0}\C ...

  3. Ubontu使用技巧

    1. ctrl + alt + T  =>  打开命令行窗口 2. sudo su => 开启root权限 3. cd  => 打开文件夹 4. cd "Program F ...

  4. 分享:JS比较两个日期大小

    发布:thatboy   来源:Net     [大 中 小] 本文介绍下,在javascript代码中,比较两个日期大小的方法,有需要的朋友参考下. 转自:http://www.jbxue.com/ ...

  5. 强大的网络通信框架(不实现缓存)--第三方开源--AsyncHttpClient

    AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-http但是Asyn ...

  6. VB最新使用教程

    Visual Basic是一种由 微软公司开发的结构化的.模块化的.面向对象的.包含协助开发环境的事件驱动为机制的可视化程序设计语言.这是一种可用于微软自家产品开发的语言.它源自于BASIC编程语言. ...

  7. Sandcastle是什么

    如果你的项目是.net开发,同时需要生成HTML的方法成员文档时,哪么就不得不拿出Sandcastle 因为Sandcastle是微软开发,并开源的文档生成工具; 这种生成进度等待的感觉很爽! 在这里 ...

  8. MVVM学习

    1:MVVMLight中通过IOC注册的服务或者是类是全局的整个工程都能访问到 2:向下的结构(viem→VM→Model)也不严格是这样 3:延迟SimpleIoc的注册 4:在Navigatedt ...

  9. Oracle逻辑体系:数据文件黑盒的内在洞天

    select username,session_num,tablespace from v$sort_usage; Block: 块的组成 Header:包含数据块的概要信息:块地址,块属于哪个段,还 ...

  10. 从Keil 4升级到Keil 5的工程,想返回来用Keil 4打开

    情景描述: 笔者电脑程序Keil 4升级到Keil 5,相应地,原来项目上的工程也在第一次用Keil 5打开的时候进行了升级.之后,由于客户需要开发资料,其版本为Keil 4,我尝试着用Keil 4打 ...