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. Python 的内置字符串方法(收藏专用)

    Python 的内置字符串方法(收藏专用) method 字符串 string python3.x  python 4.7k 次阅读  ·  读完需要 44 分钟 5 字符串处理是非常常用的技能,但 ...

  2. C结构体指针的初步使用

    #include <stdio.h> #include <string.h> struct Books { char title[50]; //char author[100] ...

  3. We found potential security vulnerabilities in your dependencies. Only the owner of this reposito...

    删除package-lock.json并同步到git 定义的依赖项./package-lock.json具有已知的安全漏洞 找到一个叫做.gitignore,把package-lock.json贴在这 ...

  4. Adobe Acrobat DC

    DC: document cloud [云服务] 但是Adobe document cloud包括: Acrobat DC, Adobe sign, 以及Web和移动应用程序. 参考: https:/ ...

  5. 记录一个webstorm的设置或者说小技巧

    在 html 的元素中,如果输入属性,默认会填充 引号,在 react 书写中非常不方便. 其中的JSX很多时候是不需要 quotation 的,只是需要一个 括号 {} 即可. 自己找了下webst ...

  6. HTML meta pragma no-cache 页面缓存

    HTML meta pragma no-cache 页面缓存不缓存页面(为了提高速度一些浏览器会缓存浏览者浏览过的页面,通过下面的定义,浏览器一般不会缓存页面,而且浏览器无法脱机浏览.) <me ...

  7. 多个List 或 Array 进行 合并

    1.调用 var aaa = new List<string>() { "0" }; var a1 = new List<string>() { " ...

  8. Tkinter 之PanedWindow标签

    一.参数说明 参数 作用 background(bg) 设置背景颜色 borderwidth(bd) 设置边框宽度 cursor  指定当鼠标在PanedWindow上飘过的时候的鼠标样式 handl ...

  9. Lock接口的认识和使用

    保证线程安全演进: synchronized volatile AtomicInteger Lock接口提供的方法: void lock():加锁 void unlock():解锁 void lock ...

  10. Ubuntu16.04启动tomcat缓慢问题之解决方案

    问题信息: -May- ::] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of Secur ...