题目:

二叉树的前序遍历

给出一棵二叉树,返回其节点值的前序遍历。

样例

给出一棵二叉树 {1,#,2,3},

   1
\
2
/
3

返回 [1,2,3].

挑战

你能使用非递归实现么?

解题:

通过递归实现,根节点->左节点->右节点

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: Preorder in ArrayList which contains node values.
*/
public ArrayList<Integer> preorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> res = new ArrayList<Integer>();
res = preTurn(res,root);
return res;
}
public ArrayList<Integer> preTurn(ArrayList<Integer> res ,TreeNode root){
if(root==null)
return res;
if(root!=null){
res.add(root.val);
if(root.left!=null){
res= preTurn(res,root.left);
}
if(root.right!=null){
res = preTurn(res,root.right);
}
}
return res;
}
}

总耗时: 1094 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: Preorder in ArrayList which contains node values.
"""
def preorderTraversal(self, root):
# write your code here
res = []
res = self.preorderTurn(res,root)
return res def preorderTurn(self,res,root):
if root==None:
return res
if root!=None:
res.append(root.val)
if root.left!=None:
res = self.preorderTurn(res,root.left)
if root.right!=None:
res = self.preorderTurn(res,root.right)
return res;

总耗时: 335 ms

非递归程序,直接来源

Java程序:

public class Solution {
/**
* @param root: The root of binary tree.
* @return: Preorder in ArrayList which contains node values.
*/
public ArrayList<Integer> preorderTraversal(TreeNode root) {
// write your code here
ArrayList<TreeNode> p = new ArrayList<TreeNode>();
ArrayList<Integer> res = new ArrayList<Integer>();
while (root != null || p.size() != 0){
res.add(root.val);
if (root.right != null)
p.add(root.right);
root = root.left;
if (root == null && p.size() != 0){
root = p.get(p.size()-1);
p.remove(p.size()-1);
}
}
return res; }
}

总耗时: 1473 ms

Python程序:

class Solution:
"""
@param root: The root of binary tree.
@return: Preorder in ArrayList which contains node values.
"""
def preorderTraversal(self, root):
# write your code here
res = []
p = [root]
while root is not None or len(p) != 1:
res.append(root.val)
if root.right is not None:
p.append(root.right)
root = root.left
if root == None and len(p) != 1:
root = p[len(p) - 1]
del p[len(p) - 1]
return res

总耗时: 230 ms

lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历的更多相关文章

  1. LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...

  2. 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)

    这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...

  3. 144 Binary Tree Preorder Traversal 二叉树的前序遍历

    给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3],   1    \     2    /   3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...

  4. Leetcode144. Binary Tree Preorder Traversal二叉树的前序遍历

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

  5. LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium

    题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...

  6. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  7. 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

    题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...

  8. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

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

  9. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

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

随机推荐

  1. 关于LINQ一个简单例子

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  2. 【zendstudio】如何利用zendstudio新建 或导入php项目

    一.利用ZendStudio创建 PHP Project 1. 打开ZendStudio, 选择:File à New à PHP Project, 如下图所示: 于是弹出如下界面: 在”Projec ...

  3. Translation002—Package Index(Android包索引)

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 看本翻译前请您注意: 本人初学android,可能有的翻译不是非常准确,但本人尽最大努力,不清楚处会做标记,并附 ...

  4. vs2010中的反编译

    有这样的需求,一个.dll文件,如何查看里面的代码呢?网上有很多关于反编译的运用的. http://blog.csdn.net/lyflcear/article/details/8102057 昨天( ...

  5. [转]PLS-S-00201, identifier 'CALLDEMO.GET_EMPLOYEES' must be declared 预编译错误原因及解决办法

    $ proc sample9.pc SQLCHECK=SEMANTICS Pro*C/C++: Release 11.2.0.1.0 - Production on Tue Jan 8 15:18:4 ...

  6. [转]Oracle学习记录 九 Prc C学习

    经过前面的了解,现在想用C语言来编程了,搜索了很多东西,后来决定先用Pro C来进行学习 在安装完Oracle数据库后就可以进行编程了,里面有一个命令proc就是对程序进行预编译的. 在这记一下,这是 ...

  7. (转)《深入理解java虚拟机》学习笔记10——并发编程(二)

    Java的并发编程是依赖虚拟机内存模型的三个特性实现的: (1).原子性(Atomicity): 原子性是指不可再分的最小操作指令,即单条机器指令,原子性操作任意时刻只能有一个线程,因此是线程安全的. ...

  8. 编辑器未包含main类型解决方法

    将文件移到 src 这个 Java Source Folder 下面去,现在在外面的 java 文件不会被当成一个需要编译的类,eclipse 不会编译 Java Source Folder 外面的任 ...

  9. 【学习总结】声明变量在@interface括号中与使用@property的区别

    方式一:直接在.h文件@interface中的大括号中声明. @interface Test : NSObject { NSString *str; // 私有变量 , 其他类无法访问,只能够该类内部 ...

  10. div+css登陆界面案例2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...