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. 对比git pull和git pull --rebase

    1.使用下面的关系区别这两个操作:git pull = git fetch + git mergegit pull --rebase = git fetch + git rebase 2 一.基本 g ...

  2. @Autowired注解(转)

    5.6.4 @Autowired注解 自Spring诞生以来,

  3. js一些小知识点

    1.isNaN(),里面传一个参数,用来判断传入的值是否是数字.可以用来做一些简单的表单判断. 2.用innerHTML属性可以操作(包括获取和设置)span的内容,实际上对所有非表单元素都可以用in ...

  4. 简单的java程序通过对话框输出 计算加减乘除运算(运算方法可选择)

    import javax.swing.JOptionPane; // import class JOptionPane public class Addition { public static vo ...

  5. jquery里面的attr和css来设置轮播图竟然效果不一致

    /*封装$*/ // window.$=HTMLElement.prototype.$=function(selector){ // var elems=(this==window?document: ...

  6. SpringMVC初步——HelloWorld的实现

    开通博客园好几个月了,今天开始要用博客园记录自己的学习过程! 目录: 导包: 1. 配置web.xml文件的springDispatcherServlet 在xml中 alt+/ 找到springdi ...

  7. fopen()函数以"a+"方式打开一个不存在的文件后读写出现问题

    问题:在完成课后习题的时候,使用fopen()函数以"a+"方式打开一个不存在的文件时,写入.读取出现错误: //添加用户输入单词后,在单词头加入编号,确保编号跟着前面的开始排序 ...

  8. webapp在Android中点击链接的时候会有淡蓝色的遮罩层

    body{-webkit-tap-highlight-color: rgba(0,0,0,0);}

  9. 基于Debian系统配置Nginx环境的Node.js应用教程

    Node.js,是当前比较流行的能够动态的快速响应内容的JavaScript框架,在有些环境下比我们使用的PHP应用都能够提高效率.目 前,Node.js可以与我们常用的Nginx.Apache等服务 ...

  10. 【Sort】QuickSort

    快速排序,平均运行时间O(N log N),最坏运行时间O(N^2). 我觉得先看Python版的快排算法(http://www.cnblogs.com/fcyworld/p/6160558.html ...