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"]

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

这个题目的难点在与分裂路径,我们在遍历到一个节点的时候需要把到达它的路径告诉它,如果这个节点是叶子节点,则把它加入路径然后添加到路径集合中。如果它不是叶子节点,则把它添加到路径中,并且继续遍历它的子节点。代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<String> list = new ArrayList(); public List<String> binaryTreePaths(TreeNode root) {
if(root == null) return list;
String s = "";
path(root, s);
return list;
} public void path(TreeNode root, String p){
String s = new String(p);
if(root.left==null && root.right==null){//如果是叶子节点
s = s + root.val;
list.add(s);
}
else s = s + root.val + "->";
if(root.left!=null) //如果左子树非空
path(root.left, s);
if(root.right!=null) //如果右子树非空
path(root.right, s);
}
}

LeetCode OJ 257. Binary Tree Paths的更多相关文章

  1. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  2. [LeetCode&Python] Problem 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❤python】 257. Binary Tree Paths

    深度优先搜索 # Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         se ...

  4. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  5. LeetCode 257. Binary Tree Paths (二叉树路径)

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

  6. 【一天一道LeetCode】#257. Binary Tree Paths

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

  8. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  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. HTTP中的重定向和请求转发的区别

    原文出处:http://blog.csdn.net/meiyalei/article/details/2129120 一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下: reque ...

  2. CocoaPods 报错 [!] The dependency `JSONModel (~> 1.2.0)` is not used in any concrete target.

    当用CocoaPods  pod install 时出现了下面的错误时: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; col ...

  3. Android Studio Gradle project refresh failed No such property classpath for class

    新建的一个 android 项目居然发现不能运行,gradle 无法启动,奇怪: Gradle 'Meitian' project refresh failed:          No such p ...

  4. 统计C语言程序行数

    补充前一篇中统计C语言程序行数的程序 写得比较匆忙,可能有些失误,等弄明白GitHub的用法并完善程序后再补充完整代码链接 没有写成函数,但经过简单修改可以作为一个计算或判断函数使用 判断算法主要为以 ...

  5. iOS开发怎么样做第三方登陆(友盟社会化分享)

    基于前一篇文章   自定义UI后 实现如下代码   即可 //第三方登陆 //    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformMa ...

  6. [SQL基础教程]1-4 SQL 表的创建

    [SQL基础教程]1-4 SQL 表的创建 创建数据库 语法 CREATE DATABASE <数据库名称> // example CREATE DATABASE shop; 创建表 语法 ...

  7. luci-bwc

    文件位于:   ../feeds/luci/modules/admin-full/src/luci-bwc.c 功能: Very simple bandwidth collector cache fo ...

  8. C/C++ - <string> 与<string.h>、<cstring>的区别

    <string.h><string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <string><string>是C ...

  9. hdu 1028 Ignatius and the Princess III(母函数入门+模板)

    Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...

  10. jQuery控制表格行移动,序号不变

    @model Gd.NetSign.Controllers.DTO.SysPamaterDTO @{ ViewBag.Title = "SysPamatList"; //Layou ...