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. 寒假day03-python

    今天完成了毕设登录注册.填报等功能的界面优化,同时总结了python网络课程的部分知识点 1.修改jupyter notebook默认路径:进入cmd:(1)cd /d F:\(2)jupyter n ...

  2. Linux系统的限制

    1.总结系统限制有:    /proc/sys/kernel/pid_max #查系统支持的最大线程数,一般会很大,相当于理论值    /proc/sys/kernel/thread-max    m ...

  3. 【范式与函数依赖】3NF与BCNF的区别

    *本文中码指代候选码,主属性为构成码的属性. 先简要引入几个概念 图1 图2 单拿出来我认为不是很好理解的3NF和BCNF详细的说说. 书上写了,BCNF是完善后的3NF.从图2中显然得出,1-3NF ...

  4. MySql索引机制

    第一部分 MySQL数据库索引的数据结构及算法理论 第二部分 MySQL索引实现机制 第三部分 MySQL中高性能使用索引的策略 数据结构及算法 MySQL官方对索引的定义为:索引(Index)是帮助 ...

  5. tensorflow deeplabv3 训练自己的数据集

    https://blog.csdn.net/malvas/article/details/90776327

  6. 33)new和delete关键字

    ---------------------------------------------------------------------------------------------------- ...

  7. jackson解析处理JSON

    package com.ruoyi.common.json; import java.io.File; import java.io.IOException; import java.io.Input ...

  8. ElasticSearch 本机可以访问,外网无法访问----问题解决

    问题:本机可以访问,外网无法访问 config/elasticsearch.yml network.host: 0.0.0.0 使用普通用户zuoys,重启es,报错如下: [1]: max file ...

  9. linux epoll 任务队列多线程模型

    /* * *EPOLL ET 触发必须使用非阻塞,LT触发可以阻塞/非阻塞. *read 函数 非阻塞读需 忙轮寻 soket关闭返回0,循环读完数据 *如果已经读完再读read返回 -1,errno ...

  10. $.proxy和$.extend

    $.proxy用法详解 参考:https://www.cnblogs.com/alice626/p/6004864.html jQuery中的$.proxy官方描述为: 描述:接受一个函数,然后返回一 ...