本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42876699

Given a binary tree, return the preorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

思路:

(1)题意为前序遍历二叉树。遍历顺序为根—>左—>右。

(2)考虑到用递归比较简单,本文使用递归的思想进行解决,由于比较简单这里不累赘,详见下方代码。

(3)希望本文对你有所帮助。

算法代码实现如下:

/**
 * @author liqq
 */
public List<Integer> preorderTraversal(TreeNode root) {
	List<Integer> result = new LinkedList<Integer>();

	if (root != null) {
		result.add(root.val);
		pre_order(result, root.left);
		pre_order(result, root.right);
	}

	return result;
}

private void pre_order(List<Integer> result, TreeNode curr) {
	if (curr != null) {
		result.add(curr.val);
		pre_order(result, curr.left);
		pre_order(result, curr.right);
	}
}

Leetcode_144_Binary Tree Preorder Traversal的更多相关文章

  1. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  2. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  3. 3月3日(3) Binary Tree Preorder Traversal

    原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的..  啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...

  4. Binary Tree Preorder Traversal on LeetCode in Java

    二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...

  5. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  6. [LeetCode] N-ary Tree Preorder Traversal N叉树的前序遍历

    Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary  ...

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

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

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

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

  9. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

随机推荐

  1. Android图表库MPAndroidChart(六)——换一种思考方式,水平条形图的实现过程

    Android图表库MPAndroidChart(六)--换一种思考方式,水平条形图的实现过程 一.基本实现 我们之前实现了条形图,现在来看下水平条形图是怎么实现的,说白了就是横起来,看下效果: 说起 ...

  2. Ribbon WorkBench 当ValueRule的值为空时的设置

    在定制Ribbon按钮的规则的时候,有时需要根据某个字段值是否为空不设定Ribbon按钮的Display rules或Enable Rules,根据Crm的版本的不同,设置有所差别: 对于Dynami ...

  3. 一张图带你看懂SpriteKit中Update Loop究竟做了神马!

    1首先Scene中只有开始一点时间用来回调其中的update方法 ;] 2然后是Scene中所有动作的模拟 3接下来是上一步完成之后,给你一个机会执行一些代码 4然后是Scene模拟其中的物理世界 5 ...

  4. Linux之dmesg命令

    功能说明:显示内核缓冲区系统控制信息的工具 ,比如系统在启动时的信息会写到/var/log/中.语 法:dmesg [-cn][-s <缓冲区大小>] 补充说明:kernel会将开机信息存 ...

  5. Android判断当前系统语言

    Android获取当前系统语言 getResources().getConfiguration().locale.getCountry() 国际化常用语言 中文: getResources().get ...

  6. spring @Qualifier注解使用

    @Autowired是根据类型进行自动装配的.如果当Spring上下文中存在多个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在U ...

  7. layout文件夹中activity_main.xml与fragment_main.xml文件的处理记录

    androidSDK更新到22.6后新建立项目时在layout文件夹下面出现了activity_main.xml与fragment_main.xml,这是为了在平板开发中使用碎片,但是让不需要碎片的人 ...

  8. 1.关于QT中json数据处理和密码md5加密

     新建一个Qt空项目 17Json.pro HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += widgets gui MyWidget ...

  9. 安卓IPC机制之Binder详解

    IPC(Inter-Process Communication,跨进程通信)是指两个进程之间数据交换的过程,因此我们首先必须了解什么是进程,什么是线程. 进程:进程是正在运行的程序的实例,与程序相比, ...

  10. Linux中printk()实例

    新建hello.c #include <linux/kernel.h> #include <linux/module.h> int init_module(void) { pr ...