给定一棵二叉树,返回其节点值的前序遍历。
例如:
给定二叉树[1,null,2,3],
   1
    \
     2
    /
   3
返回 [1,2,3]。
注意: 递归方法很简单,你可以使用迭代方法来解决吗?
详见:https://leetcode.com/problems/binary-tree-preorder-traversal/description/

Java实现:

递归实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<Integer>();
if(root==null){
return res;
}
return preorderTraversal(root,res);
}
private List<Integer> preorderTraversal(TreeNode root,List<Integer> res){
if(root==null){
return res;
}
res.add(root.val);
preorderTraversal(root.left,res);
preorderTraversal(root.right,res);
return res;
}
}

非递归实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<Integer>();
if(root==null){
return res;
}
Stack<TreeNode> stk=new Stack<TreeNode>();
stk.push(root);
while(!stk.isEmpty()){
root=stk.pop();
res.add(root.val);
if(root.right!=null){
stk.push(root.right);
}
if(root.left!=null){
stk.push(root.left);
}
}
return res;
}
}

144 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. C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)

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

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

    题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...

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

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

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

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

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

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

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

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

  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. 144. Binary Tree Preorder Traversal (二叉树前序遍历)

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

随机推荐

  1. passive aggressive(pa)和average perceptron(ap)

    passive aggressive(pa)和average perceptron(ap)

  2. Struts2的工作原理(图解)详解

    Struts2的工作原理 上图来源于Struts2官方站点,是Struts 2 的整体结构. 一个请求在Struts2框架中的处理大概分为以下几个步骤(可查看源码:https://github.com ...

  3. Codility经典算法题之九:MissingInteger

    Task description: This is a demo task. Write a function: that, given an array A of N integers, retur ...

  4. atexit函数的使用【学习笔记】

    #include "apue.h" static void my_exit1(void); static void my_exit2(void); int main(void) { ...

  5. 织梦dedecms内页分类频道友情链接实现方法

    本文介绍了织梦dedecms中内页分类频道加友情链接的三种方法,有需要的朋友参考下. 织梦dedecms中内页分类频道加友情链接,方法有三种: 先讲方法,后讲原理: 方法:先找到首页模版index.h ...

  6. java时间类型转换 JsonValueProcessor

    问题描述: java里面时间类型转换成json数据就成这样了:"createTime":{"date":30,"day":3,"h ...

  7. golang OOP面向对象

    摘自:http://www.01happy.com/golang-oop/ golang中并没有明确的面向对象的说法,实在要扯上的话,可以将struct比作其它语言中的class. 类声明 1 2 3 ...

  8. 尚观Linux最佳入门高清视频教程033/133/253

    [高清]Linux 最佳入门ULE112- RHCE033部分高清视频教程[尚观原创] 视频简介:高清RHCE033部分是RHCE考试中的基础部分,同时也是我们Linux入门的必 备学习资料.想学好L ...

  9. cocos2dx常见32种场景切换动画

    bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init( ...

  10. Laravel 5 微信小程序扩展

    小程序官方的加解密 SDK 已经非常清楚了,只不过改成 Laravel 风格而已,仅仅相当于搬砖工.至于重复造轮子,我发现其他人的扩展解密用户信息的时候代码出错了,并且需要安装一个 Laravel 的 ...