Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

   1
/ \
2 3
\
5

All root-to-leaf paths are:

["1->2->5", "1->3"]

题目标签:Tree

  这道题目给了我们一个二叉树,让我们记录所有的路径,返回一个array string list。 我们可以另外设一个findPaths function, 代入参数是node 和 String path。在设一个List<String> res 在两个funciton之外。

  遍历所有的点,对于每一个点:

    1。如果这个点是leaf node, 到底了,那么添加path 进res。

    2。如果这个点还有left child,那么递归代入node.left 和 path + "->" + left的值。

    3。如果这个点还有right child, 那么递归代入node.right 和 path + "->" + right的值。

findPaths function也不需要return,因为如果到底了,直接加入这个path就可以了,它也不会继续递归代入了。

Java Solution:

Runtime beats 68.03%

完成日期:07/05/2017

关键词:Tree

关键点:设一个function代入参数值是node 和 String path , 利用path 来传递每一次的点

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
List<String> res = new ArrayList<String>(); public List<String> binaryTreePaths(TreeNode root)
{
if(root == null)
return res; findPaths(root, String.valueOf(root.val)); return res;
} public void findPaths(TreeNode node, String path)
{
if(node.left == null && node.right == null)
res.add(path); if(node.left != null)
findPaths(node.left, path + "->" + node.left.val); if(node.right != null)
findPaths(node.right, path + "->" + node.right.val); }
}

参考资料:

https://segmentfault.com/a/1190000003465753

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 257. Binary Tree Paths (二叉树路径)的更多相关文章

  1. [LeetCode] 257. Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  2. [leetcode]257. Binary Tree Paths二叉树路径

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  3. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

  4. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

  5. LeetCode 257. Binary Tree Paths(二叉树根到叶子的全部路径)

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  6. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  7. 257 Binary Tree Paths 二叉树的所有路径

    给定一个二叉树,返回从根节点到叶节点的所有路径.例如,给定以下二叉树:   1 /   \2     3 \  5所有根到叶路径是:["1->2->5", " ...

  8. 【easy】257. Binary Tree Paths 二叉树找到所有路径

    http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...

  9. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. 如何定制 Calico 网络 Policy - 每天5分钟玩转 Docker 容器技术(70)

    Calico 默认的 policy 规则是:容器只能与同一个 calico 网络中的容器通信.本节讨论如何定制 policy. calico 能够让用户定义灵活的 policy 规则,精细化控制进出容 ...

  2. SpringMVC第二篇【过滤编码器、注解开发、requestMapping、业务方法与传统参数】

    SpringMVC过滤编码器 在SpringMVC的控制器中,如果没有对编码进行任何的操作,那么获取到的中文数据是乱码! 即使我们在handle()方法中,使用request对象设置编码也不行!原因也 ...

  3. angular之$watch方法详解

    在$apply方法中提到过脏检查,首先apply方法会触发evel方法,当evel方法解析成功后,会去触发digest方法,digest方法会触发watch方法. (1)$watch简介 在diges ...

  4. 前端系列——jquery前端国际化解决方案“填坑日记”

    前言:最近,新的平台还没有开发完成,原来的老项目又提出了新的需求:系统国际化.如果是前后端完全分离的开发模式,要做国际化,真的太简单了,有现成的解决方案,基于Node构建的时下热门的任何一种技术选型都 ...

  5. jQuery: Callbacks

    jQuery 中提供了一个Callback的工具类Callbacks,它提供了一个Callback Chain.使用它可以在一个chain上来执行相关操作.它也是jQuery中的ajax, Defer ...

  6. Go语言下载、安装、配置、使用

    Go语言 Go语言(Golang,The Go Programming Language),是谷歌2009发布的第二款开源编程语言.2009年7月份,谷歌曾发布了Simple语言,它是用来开发Andr ...

  7. 【Kafka】

    KafkaProducer Kafka消息发布客户端. 线程安全,跨线程共享单个生产者实例通常比拥有多个实例的速度更快. 例子,使用生产者发送包含序列号的字符串作为键/值对的记录: Propertie ...

  8. 【笔记】php常用函数

    phpusleep() 函数延迟代码执行若干微秒.unpack() 函数从二进制字符串对数据进行解包.uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID.time_sleep_unti ...

  9. hud 2577 How to Type

    How to Type Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. Redis缓存项目应用架构设计一

    一些项目整理出的项目中引入缓存的架构设计方案,希望能帮助你更好地管理项目缓存,作者水平有限,如有不足还望指点. 一.基础结构介绍 项目中对外提供方法的是CacheProvider和MQProvider ...