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. 吴裕雄--天生自然Linux操作系统:Linux 忘记密码解决方法

    忘记Linux系统的root密码,linux系统忘记root密码的情况该怎么办呢?重新安装系统吗?当然不用!进入单用户模式更改一下root密码即可. 步骤如下: 重启linux系统 3 秒之内要按一下 ...

  2. PAT Advanced 1051 Pop Sequence (25) [栈模拟]

    题目 Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, -, N and ...

  3. python语法基础-并发编程-线程-线程理论和线程的启动

    #######################       线程介绍         ############################## """ 线程介绍 为什 ...

  4. PowerShell创建 Profile

    profile主要用于个性化常用的函数.别名等等.每次加载powershell的时候,都会执行profile中的内容. 查看是否有profile: $profile 如果结果是false说明没有.则创 ...

  5. Linux笔记(三)——Shell编程

    预备知识 1.Shell是解释执行的脚本语言,可以直接调用Linux系统命令 2.文件以.sh结尾, #!bin/bash 标识, 说明这是一个shell脚本, 不能省略 3.执行 赋予权限,直接运行 ...

  6. SQL触发器笔记

    触发器(Trigger)是在对表进行插入.更新.删除等操作时自动执行的存储过程. 触发器是一种特殊的存储过程,它在执行语言事件时自动生效,采用事件驱动机制.当某个触发事件发生时,定义在触发器中的功能将 ...

  7. 吴裕雄--天生自然python Google深度学习框架:Tensorflow实现迁移学习

    import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...

  8. 表查询语句及使用-连表(inner join-left join)-子查询

    一.表的基本查询语句及方法 from. where. group by(分组).having(分组后的筛选).distinct(去重).order by(排序).  limit(限制) 1.单表查询: ...

  9. MySQL_语句

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname 3.说明:备份sql server--- 创建 ...

  10. android打飞机游戏、MVP句子迷App、悬浮窗、RxJava+Retrofit、加载动画、定制计划App等源码

    Android精选源码 微信打飞机 android进度设置加载效果源码 Android新手引导库EasyGuide MVP-好看又好用的句子迷客户端 XFloatView 一个简易的悬浮窗实现方案 a ...