当前位置:网站首页>Leetcode 113: path sum II
Leetcode 113: path sum II
2022-07-29 01:49:00 【Swarford】
subject :
Ideas :dfs
The idea is similar to change , Every time you recurse down , Add up the number you want sum Subtract the current node root Of val, In this way, we can get the money we need to collect from the next layer , Until the leaf node sum Equal to node will path Add to r !
Because you want to add nodes all the way from the root node to path, So it's preorder traversal ;
To get multiple paths , Use similar permutations :① choice ② recursive ③ withdraw
When choosing , Meeting the conditions will path Add to r, Recall after traversal , Maintain a path The sum used to store paths ;
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;
}
// choice
path.add(root.val);
// If it is a leaf node and meets sum Just add !
if(root.left==null && root.right==null && sum==root.val){
r.add(new LinkedList(path));
}
// recursive
dfs(root.left,sum-root.val);
dfs(root.right,sum-root.val);
// withdraw
path.removeLast();
}
}
边栏推荐
- What are source code, inverse code and complement code
- With the explosive growth of digital identity in 2022, global organizations are facing greater network security
- StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
- Internship: tool class writing for type judgment
- golang run时报undefined错误【已解决】
- Introduction to Elmo, Bert and GPT
- T-sne降维
- 规划数学期末模拟考试一
- Openpyxl border
- [understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
猜你喜欢

StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?

Introduction to Elmo, Bert and GPT
![About df['a column name'] [serial number]](/img/e2/179fb4eda695726e87bb483f65e04e.png)
About df['a column name'] [serial number]

How to choose professional, safe and high-performance remote control software

Reinforcement learning (III): dqn, nature dqn, double dqn, with source code interpretation

【10点公开课】:快手GPU/FPGA/ASIC异构平台的应用探索

Analyze OP based on autoware_ global_ Planner global path planning module re planning

CSDN modify column name

知道创宇上榜CCSIP 2022全景图多个领域

【HCIP】重发布及路由策略的实验
随机推荐
New upgrade: get Taobao product details "advanced version" API
SQL question brushing: find the last of all employees_ Name and first_ Name and corresponding department number Dept_ no
Read the recent trends of okaleido tiger and tap the value and potential behind it
[网鼎杯 2020 朱雀组]Nmap
【搜索】—— DFS之剪枝与优化
AlphaFold揭示了蛋白质结构宇宙-从近100万个结构扩展到超过2亿个结构
Sigma-DSP-OUTPUT
Openpyxl cell center
Cross modal alignment 20220728
5g commercial third year: driverless "going up the mountain" and "going to the sea"
【7.21-26】代码源 - 【平方计数】【字典序最小】【“Z”型矩阵】
MySQL execution order
使用POI,实现excel文件导出,图片url导出文件,图片和excel文件导出压缩包
CSDN modify column name
Nacos installation guide on win system
新生代公链再攻「不可能三角」
我们总结了 3 大Nacos使用建议,并首次公开 Nacos 3.0 规划图 Nacos 开源 4 周年
[hcip] two mGRE networks are interconnected through OSPF (ENSP)
【HCIP】MGRE环境下OSPF实验,含多进程双向重发布及OSPF特殊区域
PCL 点云转强度图像