当前位置:网站首页>【LeetCode 剑指 Offer 34. 二叉树中和为某一值的路径(中等)】
【LeetCode 剑指 Offer 34. 二叉树中和为某一值的路径(中等)】
2022-04-22 23:10:00 【Minaldo7】
题目:
给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。


提示:
树中节点总数在范围 [0, 5000] 内
-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题过程:
dfs
/** * 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 {
List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> pathSum(TreeNode root, int target) {
dfs(root, target, new LinkedList<>());
return res;
}
private void dfs(TreeNode root, int target, List<Integer> list){
if(root==null) return;
// 递归前把路径值放入列表中
list.add(root.val);
dfs(root.left, target-root.val, list);
dfs(root.right, target-root.val, list);
// 回溯前判断
if(root.left==null && root.right==null && target == root.val){
res.add(new LinkedList<>(list));
}
// 倒退
list.remove(list.size()-1);
}
}
执行结果:

版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Su_Del/article/details/124353587
边栏推荐
- WebRTC系列-WebRTC基础(七)NAT、stun和turn(1)
- 【板栗糖GIS】编程的本质—(视频笔记)
- VI / VIM 编辑器基本操作
- Numeric type and sequence type
- Shell script command results are saved to variables with line breaks
- public speaking
- Redis部署
- Future source code | Professor Wu Enda's lecture: Tips for using a data centric AI approach
- 登录功能&新增文章功能的测试点提取以及测试用例编写
- [原创][开源]C# Winform DPI自适应方案,SunnyUI三步搞定
猜你喜欢

解决require is not defined的报错问题
![[swift] code triggers uibutton click event](/img/23/444cfab44afe32d0476ba26f64bfdf.png)
[swift] code triggers uibutton click event

Canal使用流程、部署安装文档

面试官:ArrayList和LinkedList在性能方面的差别,能有多大?
![[原创][开源]C# Winform DPI自适应方案,SunnyUI三步搞定](/img/06/e1fec4a87eca3142d54d09844c629f.png)
[原创][开源]C# Winform DPI自适应方案,SunnyUI三步搞定

MySQL configuration adjustment: case insensitive

一个快速追踪密切接触者的开源脚本方案

VI / VIM 编辑器基本操作

.net 6 在退出构造函数时,不可为 null 的 属性“xxx”必须包含非 null 值。
![[reproduction of thesis code] errors encountered in the translation embeddings for modeling multi relational data](/img/80/43b7cedaa4c5e98cac61c79dfa37bd.png)
[reproduction of thesis code] errors encountered in the translation embeddings for modeling multi relational data
随机推荐
Detailed explanation of SQL language
企业微信扫码登录后台管理系统,如何修改二维码样式
Introduction to padding in packet encryption (pkcs1padding / pkcs5padding / iso10126padding)
[HCTF 2018]admin之unicode欺骗
English | day13,14 x sentence true research daily sentence (parallel and nested structure)
bullwhip effect
When creating a dynamic link library, the header file reports an error. Expected initializer before "function name"“
Gentoo系统安装步骤详解
从proc查询uid
Day81(动态规划、十叉树遍历)
《动态规划入门》刷题笔记(更新中)
华为机试题——HJ73 计算日期到天数转换
News Express I mobtech passed the "special safety evaluation" of China Academy of information and communications
Vtkvertex vertex
解决require is not defined的报错问题
面试官:ArrayList和LinkedList在性能方面的差别,能有多大?
Importance of safety product design
SQL Net message from client 事件产生的原因分析
LeetCode 414. 第三大的数(简单、数组)day13
how to become professional