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

Note: A leaf is a node with no children.

Example:

  1. Input:
  2.  
  3. 1
  4. / \
  5. 2 3
  6. \
  7. 5
  8.  
  9. Output: ["1->2->5", "1->3"]
  10.  
  11. Explanation: All root-to-leaf paths are: 1->2->5, 1->3
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public List<String> binaryTreePaths(TreeNode root) {
  12. List<String> res = new ArrayList<>();
  13. if (root == null) {
  14. return res;
  15. }
  16. helper(root, res, "");
  17. return res;
  18. }
  19.  
  20. private void helper(TreeNode root, List<String> res, String str) {
  21. if (root == null) {
  22. return;
  23. }
  24. if (root.left == null && root.right == null) {
  25. res.add(str + root.val);
  26. return;
  27. }
  28. String newStr = str + root.val + "->";
  29. helper(root.left, res, newStr);
  30. helper(root.right, res, newStr);
  31. }
  32. }

[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. POJ 2976 Dropping tests【0/1分数规划模板】

    传送门:http://poj.org/problem?id=2976 题意:给出组和,去掉对数据,使得的总和除以的总和最大. 思路:0/1分数规划 设,则(其中等于0或1) 开始假设使得上式成立,将从 ...

  2. web前端学习 roadmap

    <tag attribute="value">content</tag>

  3. php 查看接口运行时间

    代码如何: <?php $start_time = microtime(true); for ($i=0;$i<100000000;$i++){}; $end_time = microti ...

  4. 题解 P2016 【战略游戏】

    题目 解法跟 dalao @real_ljs 类似,但没有用到递归 [分析] 题目相当于需要求覆盖这颗树需要的最小点数 用 \(Dp_{i,0/1}\) 表示在这棵树中,以 \(i\) 为根节点的子树 ...

  5. 吴裕雄--天生自然 PHP开发学习:表单 - 验证邮件和URL

    $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $ ...

  6. PAT Basic 1083 是否存在相等的差 (20) [hash映射,map STL]

    题目 给定 N 张卡⽚,正⾯分别写上 1.2.--.N,然后全部翻⾯,洗牌,在背⾯分别写上 1.2.--. N.将每张牌的正反两⾯数字相减(⼤减⼩),得到 N 个⾮负差值,其中是否存在相等的差? 输⼊ ...

  7. keyword排序-Es问题

    问题:mapping索引registerordercount字段设置为keyword,但是在进行倒序排的视乎发现,没有按预期排序. keyword类型: "registerordercoun ...

  8. 洛谷 P1258 小车问题

    题目传送门 解题思路: 首先,每个人都要做一次车,而且两个人要同时到达,这样才能使总时间最短. 那么,我们设起点为A,终点为B,小车先带甲开到C点后甲下车走到B点,同时小车掉头与已经走到D点的乙相向而 ...

  9. JVM学习思维导图

  10. OutOfMemoryError异常

    1.Java堆溢出 Java堆用于存储对象实例,只要不断地创建对象,并且保证GC Roots到对象之间有可达路径来避免垃圾回收机制清除这些对象,就会在对象数量达到最大堆的容量限制后产生内存溢出异常. ...