当前位置:网站首页>leetcode 257. Binary tree paths all paths to a binary tree (simple)
leetcode 257. Binary tree paths all paths to a binary tree (simple)
2022-06-11 22:12:00 【InfoQ】
One 、 The main idea of the topic

Two 、 Their thinking
3、 ... and 、 How to solve the problem
3.1 Java Realization
/**
* 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);
}
}
}
Four 、 Summary notes
- 2022/6/11 Finish the first few intermediate search questions , It's easy to do something simple
边栏推荐
- Go IO module
- Leetcode stack topic summary
- Internet of things development practice 18 scenario linkage: how does an intelligent light perceive light? (I) (learning notes)
- 启牛商学院送华泰账户安不安全?真的吗
- 超標量處理器設計 姚永斌 第2章 Cache --2.4 小節摘錄
- crontab中定时执行shell脚本
- 什么是死锁?(把死锁给大家讲明白,知道是什么,为什么用,怎么用)
- How to view computer graphics card information in win11
- All inherited features
- win11怎么看电脑显卡信息
猜你喜欢

Nmap performs analysis of all network segment IP survivals in host detection

Explain asynchronous tasks in detail: the task of function calculation triggers de duplication

BUUCTF(5)
![[niuke.com] DP30 [template] 01 Backpack](/img/a2/9bcfbe6f78f30282fd8940c57477b1.jpg)
[niuke.com] DP30 [template] 01 Backpack

【学术相关】申请审核制下,到双一流大学读博的难度有多大?

Maze problem in C language

Leetcode - day 2

Take off efficiently! Can it be developed like this?

【数据挖掘时间序列分析】餐厅销量预测

Classes and objects (1)
随机推荐
利用SecureCRTPortable脚本功能完成网络设备的数据读取
实现栈和队列
Static PVC with CEPH CSI
inner join执行计划变了
剑指offer数组题型总结篇
Study notes of mattlotlib and Tkinter (I)
[niuke.com] DP30 [template] 01 Backpack
Stack栈的实现
If I take the college entrance examination again, I will study mathematics well!
机器学习之Logistic回归简单实例
Example of using zypper command
C language to achieve eight sorts (2)
Sword finger offer array question type summary
重温c语言一
使用VBScript读取网络的日志数据进行处理
The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace
Tkinter学习笔记(二)
[Yu Yue education] calculus of Zhejiang University in autumn and winter 2021 (I) reference materials
Internet of things development practice 18 scenario linkage: how does an intelligent light perceive light? (I) (learning notes)
R语言书籍学习03 《深入浅出R语言数据分析》-第八章 逻辑回归模型 第九章 聚类模型