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. Anaconda 添加清华源与恢复默认源

    1.添加清华源 查看清华大学官方镜像源文档:https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/ 本文基于Windows平台,首先用命令: conda ...

  2. dfs--汉诺塔

    在研究汉诺塔问题时,我们可以先分析俩个盘子的方法: 1.把第一个盘子放到辅助柱子上 2.把第二个盘子放大目标柱子上 3.把第一个盘子从辅助柱子移到目标柱子上 由此我们可以通过整体思想推导出一共有n个盘 ...

  3. JunOS SRX firewal Web authentication(转)

    转载自:https://srxasa.wordpress.com/2011/12/11/junos-srx-firewal-web-authentication/ JunOS SRX firewal ...

  4. 01 语言基础+高级:1-4 接口与多态_day09【继承、super、this、抽象类】

    day09[继承.super.this.抽象类] 三大特性——继承方法重写super关键字this关键字抽象类 教学目标能够解释类名作为参数和返回值类型能够写出类的继承格式能够说出继承的特点能够说出子 ...

  5. unity学习 5.x依赖打包和解包

    unity5已经封装好了接口,所以依赖打包并没有那么神秘和复杂了. 打包: 1.定义好资源的assetBundleName 2.BuildPipeline.BuildAssetBundles,指定资源 ...

  6. <黑马新秀>Spring学习日志

    # 用于梳理Spring知识点 Spring是分层的Java EE应用全栈轻量级开源框架,以IoC(Inverse Of Control反转控制)和AOP(Aspect Oriented Progra ...

  7. dbgview 在windows 10 中关闭后再次打开时无法“capture kernel”

    DbgView 是一个免费的用于抓取log 的工具,可以捕获并输出OutputDebugString()函数的输出,以及输出windows driver 中dbgprint 的log,对于window ...

  8. opencv---颜色空间转化并实现物体跟踪

    一.图像处理的基本操作 因为这是第一篇写opencv的笔记,故先讲讲在python下写opencv的基本操作.总共总结了三点如下: 开头一定要加编码声明:-*- coding: utf-8 -*- p ...

  9. 基础篇九:模块介绍(--with-http_stub_status_module)

    下面--with 即为编译安装的模块 下面我们来介绍--with-http_stub_status_module此模块 vim  /etc/nginx/conf.d/default.conf 然后检查 ...

  10. C++ 类中使用dllimport和dllexport

    在Windows平台下: 您可以使用dllimport或dllexport属性声明C ++类.这些形式意味着导入或导出整个类.以这种方式导出的类称为可导出类. 以下示例定义可导出的类.导出其所有成员函 ...