题目:

二叉树的前序遍历

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

样例

给出一棵二叉树 {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. JQuery插件开发 - 模板

    (function($) { $.fn.PluginName = function(options) { // 创建一个默认设置对象 var defaults = { key : "Defa ...

  2. Web应用登出后防止浏览器后退

    通常情况下,浏览器会对页面进行缓存,此时可以通过后退访问刚才的页面,如:Web应用登出后后退能够访问刚才被缓存的页面,这样在有些情况下是不够安全的,解决防止后退的办法如下: response.setH ...

  3. Python的类实例方法,类方法,类静态方法

    以下面的类定义为例: # coding:utf-8 class A: count = 0 def __init__(self, inst_name): self.inst_name = inst_na ...

  4. Zendframework application 引导过程

    Applications 会期望用户提供一个配置好的ServiceManager.提供以下服务: 1.EventManager 2.ModuleManager 3.Request 4.Response ...

  5. EHCache 实现通用类 CacheManager

    package com.zhubaje.api.workflow.ehcache; import java.io.Serializable; import java.util.ArrayList; i ...

  6. 【mongodb】 需求

    增删改查 增加:  (表名,增加内容) 删除: (表名,数据id) 改: (表名 根据id获取数据 修改 增加 查 (表名,字段 ) 关联查询? 请主站提供一下 在使用的接口

  7. Linq小技巧

    遍历集合ForEach: listAll.Items.Clear(); List<Users> list = DP.UsersDAO.GetInfoList(); list.ForEach ...

  8. SVN服务器搭建和使用

    SVN服务器搭建和使用 Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下 ...

  9. Careercup - Facebook面试题 - 6204973461274624

    2014-05-02 02:28 题目链接 原题: I/P: N, k O/P: all subset of N with exactly K elements. eg: I/p: N = , K = ...

  10. js获取对象、数组的实际长度,元素实际个数

    /*获取对象.数组的长度.元素个数 *@param obj 要计算长度的元素,可以为object.array.string */ function count(obj){ var objType = ...