题目:

二叉树的前序遍历

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

样例

给出一棵二叉树 {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. NOSQL之【redis的安全策略】

    原文:http://redis.io/topics/security 1.Redis的安全模式 可信环境下的可信用户才可访问redis.这意味着,将redis服务器直接暴露在Internet或者不可信 ...

  2. jQuery select的操作代码

    jQuery對select的操作的实际应用代码. //改變時的事件  复制代码代码如下: $("#testSelect").change(function(){ //事件發生  j ...

  3. Eclipse导入android包错误

    错误提示:Invalid project description… 解决方案:假设你的工作空间是workshop,那么你可以在你的workshop下新建一个文件夹,然后放入你的包,再在Eclipse中 ...

  4. apache common包下的StringUtils的join方法

    apache common包下的StringUtils的join方法: 关键字:java string array join public static String join(Iterator it ...

  5. unpipc.h&unpipc.c

    unpipc.h #ifndef _UNPIPC_H #define _UNPIPC_H #include <stdio.h> #include <unistd.h> #inc ...

  6. 轻量级远程调用框架-Hessian学习笔记-Demo实现

    Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单.快捷.采用的是二进制RPC协议,因为采用的是二进制协 ...

  7. GGS: Sybase to Oracle

    Step 1: Start the GGSCI on Source and Target Source Target Oracle GoldenGate Command Interpreter for ...

  8. Build Settings

    Add Open Scenes 选择一个关卡,使其处于打开状态,在菜单栏选择 File -> Build Settings 打开Build Settings窗口.选择 Add Open Scen ...

  9. 【软件工程-Teamwork 3】团队角色分配和团队贡献分分配规则

    Part 1 团队角色分配 1.人员分配概要: Project Manager:1名 / Developer:4名 / Test: 1名 2.具体人员分配及职责: Project Manager(PM ...

  10. linux 命令小结

    chkconfig --list  查询所有服务运行情况 修改文件夹权限: 在Linux中,权限的所有者分为用户权限,组权限和其他权限,分别是用字母u, g, o 代表权限分为:读 r , 写 w , ...