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 ...
随机推荐
- 最初程序员的思维“修炼”之四——Android平台开发的“强制关闭”解决思路
我和我的朋友参加一个比赛——物联网应用技能大赛,这个大赛所要求的技能有,硬件技术,Android平台开发技术,.NET平台开发技术,所以这是一个团队合作的比赛,基本上没有人能同时掌握这三种技术(在校生 ...
- html5离线应用详摘
html5离线应用详摘 在html文件里配置如下: <html manifest=”name.manifest”> 在name.manifest文件里配置如下: CACHE MANIFES ...
- jquery插件开发模板
(function($){ $.fn.extend({ //将可选择的变量传递给方法 pluginname: function(options) { //设置默认值并用逗号隔开 var default ...
- MongoDB五种树形结构表示法
MongoDB五种树形结构表示法 第一种:父链接结构 db.categories.insert( { _id: "MongoDB", parent: "Databases ...
- JAVA多线程学习3--线程一些方法
一.通过sleep方法睡眠 在指定的毫秒数内让当前正在执行的线程休眠(暂停执行).该线程不丢失任何监视器的所属权. 二.线程优先级 线程具有优先级,范围为1-10. MAX_PRIORITY线程可以具 ...
- 文件上传下载struts2
上传方式1: // 保存上传的文件 public boolean saveFile(File file, String fileName) throws IOException { File newF ...
- 1097. Deduplication on a Linked List (25)
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...
- Linux下安装firefox的flash插件
OS:ORACLE-LINUX 5.71.下载"install_flash_player_11_linux.i386.tar.gz" 包2.自己创建个目录,解压[root@b28- ...
- 鼠标驱动之-sys节点-input子系统
首先需要了解sys节点和linux驱动编程的知识,在linux内核<linux/>下有着对应的实现.本例实现创建sys节点,外围程序通过input子系统控制鼠标位置. 第一步编写驱动代码, ...
- 在SQL SErver中实现数组功能
T-SQL象数组一样处理字符串.分割字符串 在日常的编程过程中,数组是要经常使用到的.在利用SQL对数据库进行操作时,有时就想在SQL使用数组,比如将1,2,3,4,5拆分成数组.可惜的是在T- ...