题目:

二叉树的前序遍历

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

样例

给出一棵二叉树 {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. Entity Framework 一次加载许多个 Fluent API 映射

    可通过多种方法来指定模型的 Fluent 映射(从类到数据库). 1.直接在 DbContext 类的 OnModel­Creating 方法中进行映射,如下所示: protected overrid ...

  2. 读取XML

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

  3. linux 关机方式

    linux 关机命令: 1-  init 0 关机. 具体详情接受可以 init --help 查询 如下: init [OPTIONS...] {COMMAND} Send control comm ...

  4. RxJava 平常使用

    本文转载自: http://blog.csdn.net/theone10211024/article/details/50435325 一.Scheduler线程切换 这种场景经常会在“后台线程取数据 ...

  5. How to: Enable and Disable an Action Pane Button on a List Page [AX 2012]

    Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynami ...

  6. CentOS 6.2编译安装Nginx1.2.0+MySQL5.5.25+PHP5.3.13+博客系统WordPress3.3.2

    说明: 操作系统:CentOS 6.2 32位 系统安装教程:CentOS 6.2安装(超级详细图解教程): http://www.osyunwei.com/archives/1537.html 准备 ...

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

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

  8. SVN学习

    一.SVN在线安装(Eclipse) 步骤1: 步骤2 其中http://subclipse.tigris.org/update_1.10.x是最新版本的SVN插件的下载站点[subclipse是Ec ...

  9. hibernate中增加annotation @后不提示信息【转】

    此文转自:http://blog.knowsky.com/252047.htm 所需要用到的3个jar包分别是: hibernate-annotations.jar ejb3-persistence. ...

  10. Sublime key bindings使用

    开启vi mode后,可以使用很多的VI快捷方式,所以我的sublime已经不是单纯的st了,st的VI模式不完全支持所有的快捷键.我们来看一段官网的key bindings示例: { "k ...