当前位置:网站首页>leetcode 257. Binary Tree Paths 二叉树的所有路径(简单)
leetcode 257. Binary Tree Paths 二叉树的所有路径(简单)
2022-06-11 22:03:00 【InfoQ】
一、题目大意

二、解题思路
三、解题方法
3.1 Java实现
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> ans = new ArrayList<>();
dfs(root, "", ans);
return ans;
}
void dfs(TreeNode node, String path, List<String> ans) {
boolean end = true;
if ("".equals(path)) {
path = String.valueOf(node.val);
} else {
path += "->" + node.val;
}
if (node.left != null) {
dfs(node.left, path, ans);
end = false;
}
if (node.right != null) {
dfs(node.right, path, ans);
end = false;
}
if (end) {
ans.add(path);
}
}
}
四、总结小记
- 2022/6/11 做完前几个中等的搜索题,再做简单的就很简单了
边栏推荐
- LeetCode栈题目总结
- 超標量處理器設計 姚永斌 第2章 Cache --2.4 小節摘錄
- Tkinter study notes (II)
- 每日一题-删除有序数组的重复项
- Latex combat notes 3- macro package and control commands
- R language book learning 03 "in simple terms R language data analysis" - Chapter 10 association rules Chapter 11 random forest
- Daily question - Roman numeral to integer
- Addition without addition, subtraction, multiplication, Division
- Tkinter学习笔记(二)
- Nmap进行主机探测出现网段IP全部存活情况分析
猜你喜欢
随机推荐
How to view computer graphics card information in win11
一款开源的Markdown转富文本编辑器的实现原理剖析
Go IO module
常用分页方法总结
Tkinter study notes (IV)
One question of the day - delete duplicates of the ordered array
Go OS module
C language implements eight sorts of sort merge sort
R语言书籍学习03 《深入浅出R语言数据分析》-第八章 逻辑回归模型 第九章 聚类模型
如果重来一次高考,我要好好学数学!
Collection of articles and literatures related to R language (continuously updated)
R language book learning 03 "in simple terms R language data analysis" - Chapter 8 logistic regression model Chapter 9 clustering model
crontab中定时执行shell脚本
使用VBScript读取网络的日志数据进行处理
启牛商学院送华泰账户安不安全?真的吗
高考结束,人生才刚刚开始,10年职场老鸟给的建议
R language book learning 03 "in simple terms R language data analysis" - Chapter 10 association rules Chapter 11 random forest
238. product of arrays other than itself
go encoding包
Uncover the secret of the popular app. Why is it so black








