[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说文解字_继承过程中的参数集合
1. 先看一段属性继承的代码: class User: def __init__(self,name,age): self.name = name self.age = age class User1 ...
- sklearn 模型评估
原文链接 http://d0evi1.com/sklearn/model_evaluation/ 预测值:pred 真实值:y_test #### 直接用平均值 ``` mean(pred == y_ ...
- Ubuntu的奇技淫巧
sudo apt-get install cmatrix 输入密码,安装后,按F11把terminal全屏,输入cmatrix -b sudo apt-get install sl 安装后执行sl,屏 ...
- dbus探索
一.参考网址 1.Dbus组成和原理
- Java 10按钮设计(awt)
/** * 2019年8月9日08:03:41 * 目的:利用Java设计10个按钮 * @author 张涛 * */ //导入awt包 import java.awt.*; import java ...
- pandas读取和写入excel多个sheet表单
一.读取单个表单 import pandas as pd excel_reader=pd.ExcelFile('文件.xlsx') # 指定文件 sheet_names = excel_reader. ...
- linux mysql备份数据库
$ mysqldump -u root -p 数据库名称 > beifen.sql 恢复 source beifen.sql
- 小白学习之pytorch框架(5)-多层感知机(MLP)-(tensor、variable、计算图、ReLU()、sigmoid()、tanh())
先记录一下一开始学习torch时未曾记录(也未好好弄懂哈)导致又忘记了的tensor.variable.计算图 计算图 计算图直白的来说,就是数学公式(也叫模型)用图表示,这个图即计算图.借用 htt ...
- linux下tab作用的描述?
[Tab] 接在一串指令的第一个字的后面,则为命令补全; 实例怎么描述?什么叫一串指令的第一个字?[Tab] 接在一串指令的第二个字以后时,则为『文件补齐』 实例怎么描述?什么叫一串指令的 ...
- Java任务调度框架之分布式调度框架XXL-Job介绍
Java任务调度框架之分布式调度框架XXL-Job介绍及快速入门 调度器使用场景: Java开发中经常会使用到定时任务:比如每月1号凌晨生成上个月的账单.比如每天凌晨1点对上一天的数据进行对账操作 ...