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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
if (root == null) {
return res;
}
helper(root, res, "");
return res;
} private void helper(TreeNode root, List<String> res, String str) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
res.add(str + root.val);
return;
}
String newStr = str + root.val + "->";
helper(root.left, res, newStr);
helper(root.right, res, newStr);
}
}

[LC] 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】257. Binary Tree Paths

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

  3. 257. Binary Tree Paths返回所有深度优先的遍历

    [抄题]: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

  4. Leetcode 257. Binary Tree Paths

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

  5. (easy)LeetCode 257.Binary Tree Paths

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

  6. 257. Binary Tree Paths

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

  7. Java [Leetcode 257]Binary Tree Paths

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

  8. LeetCode OJ 257. Binary Tree Paths

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

  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. 每天一杯C_Visual Studio各个版本的区别和总结

    细致区别如上图所示 企业版点满图中技能树,能够提供点对点的解决方案,充分满足正规企业的要求. PS:技能最多,肯定也就价格最贵 专业版中提供的专业开发者工具.服务和订阅就成了最佳选择. PS:技能多, ...

  2. 2019牛客网暑假多校训练第四场 K —number

    链接:https://ac.nowcoder.com/acm/contest/884/K来源:牛客网 题目描述 300iq loves numbers who are multiple of 300. ...

  3. for-each用法误区(不能改变数组元素值)

    代码例程: /**  * 数据加密传输  */ import java.util.Scanner; public class secretPass {     public static void m ...

  4. php list的用法

    <?php $my_array = array("Dog","Cat","Horse"); list($a, $b, $c) = $m ...

  5. JAVA多线程之状态转换图

    线程状态转换图如下: 1.新建(new):线程对象被创建后就进入了新建状态.如:Thread thread = new Thread(); 2.就绪状态(Runnable):也被称为“可执行状态”.线 ...

  6. 寒假day01-Spring框架

    1.什么是Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE D ...

  7. XSL使用写法与效果

    data.xml <?xml-stylesheet type="text/xsl" href="getdata.xsl"?> <ROOT> ...

  8. 吴裕雄--天生自然TensorFlow高层封装:使用TFLearn处理MNIST数据集实现LeNet-5模型

    # 1. 通过TFLearn的API定义卷机神经网络. import tflearn import tflearn.datasets.mnist as mnist from tflearn.layer ...

  9. 阿里OSS下载文件,提示The request signature we calculated does not match the signature you provided. Check your key and signing method

    提示说是签名不对,但没搞懂签名具体是啥,以为之前做过,有正确的,就一点点比对,最后发现竟然是下载的文件路径,里面必须是/,而不能是\或\\,搞得我哭笑不得.比如,要下载的文件路径是:soft/cszt ...

  10. Halcon系列(1) 菜鸟入门

    官方网站怎么使用HDevelop  :https://www.mvtec.com/products/halcon/halcon-tour