[LC] 257. Binary Tree Paths
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的更多相关文章
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- 257. Binary Tree Paths返回所有深度优先的遍历
[抄题]: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 257. Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
- Java [Leetcode 257]Binary Tree Paths
题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- LeetCode OJ 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- Python笔记_第一篇_面向过程_第一部分_5.Python数据类型之数字类型(number)
Python 数字类型(number)用于存储数值.数据类型是不允许改变的,这就意味着如果改变number数据类型的值,将重新分配内存空间. 1. 一个简单的示例: # 以下实例在变量赋值时数字类 ...
- 89.QuerySet API常用方法使用详解:count,first,last,aggregate,exists
1.count():计算数据的个数. 计算数据的个数可以使用count,在python中使用len()也可以计算数据的个数,但是相对来说效率没有使用count()效率高,因为在底层是使用select ...
- I420转RGB
http://blog.csdn.net/huiguixian/article/details/17288909 public class YuvToRGB { private static int ...
- Celery架构
Celery 官方 # Celery 官网:http://www.celeryproject.org/ # Celery 官方文档英文版:http://docs.celeryproject.org/e ...
- session和token区别
分布式系统认证/授权目前分布式系统存在两种常用的认证授权方式:分布式session和token 1.session的概念 session中存放登录用户的个人信息,创建session时,随机生成一个se ...
- pix2pix-tf官方文档
# 对抗网络的基本思想 # 假设有一种概率分布M,它相对于我们是一个黑盒子.为了了解这个黑盒子中的东西是什么,我们构建了两个东西G和D, # G是另一种我们完全知道的概率分布,D用来区分一个事件是由黑 ...
- POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)
http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...
- JavaScript中Promise 使用、原理以及实现过程
1.什么是 Promise promise 是目前 JS 异步编程的主流解决方案,遵循 Promises/A+ 方案. 2.Promise 原理简析 (1)promise 本身相当于一个状态机,拥有三 ...
- kaggle注册获取数据
安装谷歌访问助手,主要参考下面的作者写的 https://segmentfault.com/a/1190000020548973?utm_source=tag-newest 安装之后,打开蓝灯或其他翻 ...
- The equal-likelihood model|event|experiment|probability model
5.1Probability Basics uncertainty is inherent in inferential statistics,因为总是需要样本估计总体,The science of ...