当前位置:网站首页>LeetCode 113:路径总和 II
LeetCode 113:路径总和 II
2022-07-29 00:53:00 【斯沃福德】
题目:
思路:dfs
思路类似零钱兑换 ,每次向下递归时,把要凑的数sum 减去当前节点root的val,这样即得到下一层需要凑的钱,直到叶子节点的sum等于节点则将path添加到 r !
由于要从根节点一路添加节点到path,所以是前序遍历;
要得到多个路径,使用类似排列组合的方法:①选择 ②递归 ③撤回
选择的时候,满足条件就将path添加到 r,遍历完再撤回,维护一个path用来存储路径的和;
class Solution {
List<List<Integer>> r=new LinkedList<>();
LinkedList<Integer> path=new LinkedList<>();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
dfs(root,sum);
return r;
}
void dfs(TreeNode root,int sum){
if(root==null){
return;
}
// 选择
path.add(root.val);
// 如果是叶子节点且满足sum才添加!
if(root.left==null && root.right==null && sum==root.val){
r.add(new LinkedList(path));
}
// 递归
dfs(root.left,sum-root.val);
dfs(root.right,sum-root.val);
// 撤回
path.removeLast();
}
}
边栏推荐
- 易观分析:以用户为中心,提升手机银行用户体验,助力用户价值增长
- Super technology network security risk assessment service, comprehensively understand the security risks faced by the network system
- Data security is a competitive advantage. How can companies give priority to information security and compliance
- Openpyxl border
- Read the recent trends of okaleido tiger and tap the value and potential behind it
- What is the ISO assessment? How to do the waiting insurance scheme
- Embedded sharing collection 23
- Cloud native application comprehensive exercise
- numpy.where() 用法和np.argsort()的用法
- Canal real-time parsing MySQL binlog data practice
猜你喜欢

Embedded sharing collection 23

Cloud native application comprehensive exercise
![[search] - iteration deepening / bidirectional dfs/ida*](/img/88/d460276a657cfc49eff10015c29e5d.png)
[search] - iteration deepening / bidirectional dfs/ida*

body中基本标签

Plato launched the LAAS protocol elephant swap, which allows users to earn premium income

2022年最火的十大测试工具,你掌握了几个

How many of the top ten test tools in 2022 do you master

HCIA配置实例(eNSP)
![[search] - DFS pruning and optimization](/img/d4/7c2fec02f5a6bcfa2d5e204398af01.png)
[search] - DFS pruning and optimization

10 major network security incidents in the past 10 years
随机推荐
Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
JS事件简介
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
Reinforcement learning (II): SARS, with code rewriting
The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
5G 商用第三年:无人驾驶的“上山”与“下海”
[SQL's 18 dragon subduing palms] 01 - Kang long regrets: introductory 10 questions
2022年最火的十大测试工具,你掌握了几个
剑指offer专项突击版第13天
SQL question brushing: find the employee number EMP with more than 15 salary records_ No and its corresponding recording times t
Making high-precision map based on autoware (V)
[机缘参悟-54]:《素书》-1-事物缘起[原始章第一]:大道至简。
Lombook User Guide
Moonbeam上的多链用例解析——Derek在Polkadot Decoded 2022的演讲文字回顾
【搜索】—— 迭代加深/双向DFS/IDA*
Openpyxl merge cells
[hcip] two mGRE networks are interconnected through OSPF (ENSP)
了解各种路径
Openpyxl border
【golang】使用select {}