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. Bootstrap栅格系统用法--Bootstrap基础

    1.栅格系统实现布局的原理 1)Bootstrap把屏幕的宽度拆分成12格(列),每一格像素的多少由设备屏幕分辨率决定,我们在开发项目的过程中不需要去指定像素或者百分比. 2)不同范围的分辨率对应不同 ...

  2. 关于linux下的文件权限

    在ls指令加 -l 参数能看到文件权限 就像这样: drwxrwxr-x 2 asml users 4096 Jul 24 02:45 desktop 第一个d表示这是个目录,若为"-&qu ...

  3. angularjs之ui-bootstrap的Datepicker Popup实现双日期选择控件

    最开始使用ui-bootstrap的Datepicker Popup日期选择插件实现双日期选择时间范围时,在网上搜了一些通过JS去实现的方法,不过后来发现可以不必通过JS去处理,只需要使用其自身的属性 ...

  4. thinkphp5.0无限极分类及格式化输出

    首先我们来看数据表 从上图中可以发现,中国下有贵州,北京两个子节点,而北京有天安门一个子节点,纽约的子节点是"纽约的子类". 从pid为0看出,中国和纽约是顶级节点. 因为贵州的p ...

  5. oracle 例外

    一.例外分类oracle将例外分为预定义例外.非预定义例外和自定义例外三种.1).预定义例外用于处理常见的oracle错误.2).非预定义例外用于处理预定义例外不能处理的例外.3).自定义例外用于处理 ...

  6. GitHub使用(三) - GitHub安装及初步使用

    1. 首先下载Windows版本“GitHubSetup.exe”如下,下载地址为:https://desktop.github.com/

  7. textarea文本域值中含有大量\t\n问题

    最近在发现了一个问题,很是头疼,textarea值中有大量的制表符,尝试了很多办法,最终找到了解决办法,希望能帮到同样有此困扰的你. <textarea> <c:out value= ...

  8. ClassLoader类加载机制&&JVM内存管理

    一.ClassLoader类加载机制 在java中类加载是遵循委派双亲加载的:通过调用loadClass方法逐级往上传递委派加载请求,当找不到父ClassLoader时调用其findClass方法尝试 ...

  9. hdu2157矩阵快速幂

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. Ubuntu 16安装GPU版本tensorflow

    pre { direction: ltr; color: rgb(0, 0, 0) } pre.western { font-family: "Liberation Mono", ...