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

思想:递归
代码如下:
/**
* 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> binaryTreePaths(TreeNode root) {
List<String> ret=new ArrayList<>();
if(root==null) return ret;
String s=""+root.val;
paths(root,s,ret);
return ret;
}
public void paths(TreeNode root,String s,List<String>ret){
if(root.left==null && root.right==null){
ret.add(s);
}
else{
if(root.left!=null) paths(root.left,s+"->"+root.left.val,ret);
if(root.right!=null) paths(root.right,s+"->"+root.right.val,ret);
}
} }

  

运行结果:

(easy)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. For example, given the following binary tree: 1 ...

  3. Leetcode 257. Binary Tree Paths

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

  4. Java [Leetcode 257]Binary Tree Paths

    题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

  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 257. Binary Tree Paths(二叉树根到叶子的全部路径)

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

  7. Leetcode 257 Binary Tree Paths 二叉树 DFS

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

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

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

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

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

随机推荐

  1. 转:GBDT(MART) 迭代决策树入门教程 | 简介

    在网上看到一篇对从代码层面理解gbdt比较好的文章,转载记录一下: GBDT(Gradient Boosting Decision Tree) 又 叫 MART(Multiple Additive R ...

  2. (转帖)BootStrap入门教程 (一)

      2011年,twitter的“一小撮”工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端工具集--BootStrap.Bootstrap由MAR ...

  3. IOS开发—数据库的使用

    1.首先封装方法(建一个DataBase类封装) 2.实现DataBase类的方法 3.第一步先建立一个表 4.进行插入.删除.更新 插入的sql语句:@"insert into user ...

  4. C#.NET ,微信退款证书

    微信退款时遇到:基础连接已经关闭 连接被意外关闭. 服务器环境:WIN SERVER 2008 R2.  WINDOWS服务承载的WCF服务,基于.NET FRAMEWORK 3.5. 第一笔交易的退 ...

  5. bzoj2537: [neerc2007]Language Recognition

    Description DFA(确定性有限状态自动机)是一个有向图,顶点称为状态,边称为转移.每个转移用一个字母标记.对于每个状态s和每个转移l,至多有一个转移从s出发且标记为l.DFA有一个初始状态 ...

  6. php 根据指定的键对多维数组进行排序

    根据指定的键对多维数组排序,可以按照多个键排序 我们通常有一些多维数组需要排序: $guys = array( array( 'name' => 'jake', 'score' => 80 ...

  7. Perl调用外部命令的方式和区别

    主要的方式简述如下:1. system("command");使用该命令将开启一个子进程执行引号中的命令,父进程将等待子进程结束并继续执行下面的代码. 2. exec(" ...

  8. mousewheel滚轮事件 浏览器的写法

    鼠标的滚轮事件,在Jquery中有对应的一个插件:https://github.com/brandonaaron/jquery-mousewheel 原生的滚轮事件:火狐与其他浏览器使用了不同的事件 ...

  9. Javascript金额转化

    //"123,456.78"----> 123456.78(float格式) function rmoney(s) { return parseFloat(s.replace ...

  10. ADF_Database Develop系列3_通过UML进行数据库开发之将Database Diagram转为Class Diagram

    2013-05-01 Created By BaoXinjian