257. Binary Tree Paths

Easy

Given a binary tree, return all root-to-leaf paths.

Note: A leaf is a node with no children.

Example:

Input:

   1
/ \
2 3
\
5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3
package leetcode.easy;

/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class BinaryTreePaths {
java.util.List<String> list = new java.util.LinkedList<>(); public java.util.List<String> binaryTreePaths(TreeNode root) {
path(root, "");
return list;
} private void path(TreeNode root, String str) {
if (null == root) {
return;
}
str = str + "->" + root.val;
if (null == root.left && null == root.right) {
list.add(str.substring(2));
return;
}
path(root.left, str);
path(root.right, str);
} @org.junit.Test
public void test() {
TreeNode node11 = new TreeNode(1);
TreeNode node21 = new TreeNode(2);
TreeNode node22 = new TreeNode(3);
TreeNode node32 = new TreeNode(5);
node11.left = node21;
node11.right = node22;
node21.left = null;
node21.right = node32;
node22.left = null;
node22.right = null;
node32.left = null;
node32.right = null;
System.out.println(binaryTreePaths(node11));
}
}

LeetCode_257. Binary Tree Paths的更多相关文章

  1. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

  2. LintCode Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...

  3. 【LeetCode】257. Binary Tree Paths

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

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

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

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

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

  6. leetcode : Binary Tree Paths

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

  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. Binary Tree Paths

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

  9. (easy)LeetCode 257.Binary Tree Paths

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

随机推荐

  1. Nutch2.1+solr3.6.1+mysql5.6问题

    1.Nutch2.1问题 1.1 问题:导入完成后,Nutch2.1里面runtime仍旧不能运行,出现jobfailed等错误. 解决:runtime里的nutch调试过程和导入Eclipse差不多 ...

  2. 织梦关于表情符后面跟着css样式修改

    织梦关于表情符后面跟着css样式修改,前段页面是会员中心里,后台是会员心情管理 前段修改路径找到member\index_do.php  大约在396行,找不到搜索  ‘对表情进行解析’  替换下面的 ...

  3. python3文本读取与写入常用代码

    创建文件夹: import os import shutil def buildfile(echkeyfile): if os.path.exists(echkeyfile): #创建前先判断是否存在 ...

  4. Apache Kylin在美团点评的应用

      本文原载自大数据杂谈微信公众号. 感谢美团点评工程师高大月撰文并授权转载. 高大月,美团点评工程师,Apache Kylin PMC成员,目前主要在美团点评数据平台负责OLAP查询引擎的建设. 背 ...

  5. asp.net文件夹上传下载组件

    ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...

  6. 【JS】时间问题

    一.JS计算时间差 返回(天.小时.分钟.秒) var date1= '2015/05/01 00:00:00'; //开始时间,为了浏览器兼容性,最好不要用'2015-05-01 00:00:00' ...

  7. Nginx 安装配置【必须把文件到放到机器上】

    [必须把所有下载的gz文件到放到机器上:编译] 1.安装nginx之前的编译软件 yum -y install make zlib zlib-devel gcc-c++ libtool  openss ...

  8. 发现Mathematica中求逆出错

    发现Mathematica中应用Inverse求逆时出错.

  9. Lua chunk文件结构

    1.lua执行经过: xx.lua源码文件------->执行(lua虚拟机) 隐式调用luac编译器 我们可以直接用luac命令去编译lua源码文件,然后用编译后的文件运行在lvm(lua虚拟 ...

  10. docker技术入门(2)

    接上一篇文章 [容器技术]Docker容器技术入门(一) 今天接着上次聊一聊有关Docker网络.数据存储相关的技术点 Docker网络模式 01 Dokcer 通过使用 Linux 桥接提供容器之间 ...