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. MyBatis java and MySql local variables

    <insert id="create" parameterType="models.entities.CategoryEntity"> set @c ...

  2. windows证书地址

    C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys 用certutil -sto ...

  3. LVM---动态调整磁盘容量

    LVM:logical volume manager(逻辑卷管理):LVM屏蔽了底层磁盘布局,方便于动态调整磁盘容量. 一.创建逻辑卷的步骤: 1)通过fdisk 工具将磁盘转换为linux分区 2) ...

  4. sgu233 little kings

    题目大意: 有n*n的棋盘上放k个国王.国王可以攻击与它相邻的八个格子.现在要使国王不相互攻击,有多少种放置的方案数.一个格子不能放两个国王. n<=10,k<=n*n. 分析:简单的状态 ...

  5. LDAP过滤器使用说明(用户、组和容器的默认 LDAP 过滤器和属性)

    说明来源:http://docs.oracle.com/html/E35191_01/ldap-filters-attrs-users.html#ldap-filters-attributes-use ...

  6. 访问public

    public(C# 参考) public 关键字是类型和类型成员的访问修饰符. 公共访问是允许的最高访问级别. 对访问公共成员没有限制,如下例所示: class SampleClass { publi ...

  7. 黄聪:深入理解PHP Opcode缓存原理

    什么是opcode缓存? 当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode).Opcode cache的目地是避免重复编译,减少 ...

  8. Ignoring HTTPS certificates

    Our Development setups won't have valid or trusted certificates. When do you want test our webserver ...

  9. working copy locked 问题

    解法 1 :  右键svn-->clean up 解法 2 :  被lock的文件夹进入控制台 del lock /q/s [转载解法] SVN 本地更新时,由于一些操作中断更新,如磁盘空间不够 ...

  10. Service代码示例

    package com.homily.training.service; import android.app.Service; import android.content.Intent; impo ...