当前位置:网站首页>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();
}
}
边栏推荐
- 动态内存与智能指针
- Use of resttemplate and Eureka
- About df['a column name'] [serial number]
- Data security is a competitive advantage. How can companies give priority to information security and compliance
- [机缘参悟-54]:《素书》-1-事物缘起[原始章第一]:大道至简。
- ELS square movement
- numpy.where() 用法和np.argsort()的用法
- 10 major network security incidents in the past 10 years
- [observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's "flywheel effect"
- Come on, handwritten RPC S2 serialization exploration
猜你喜欢

Event express | Apache Doris Performance Optimization Practice Series live broadcast course is open at the beginning. You are cordially invited to participate!

After understanding the composition of the URL of the website, we use the URL module, querystring module and mime module to improve the static website

5g commercial third year: driverless "going up the mountain" and "going to the sea"

J9 number theory: what factors determine the value of NFT?

【HCIP】两个MGRE网络通过OSPF实现互联(eNSP)

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

【GoLang】网络连接 net.Dial

把逻辑做在Sigma-DSP中的优化实例-数据分配器

Behind the second round of okaleido tiger sales is the strategic support of ecological institutions

What is the ISO assessment? How to do the waiting insurance scheme
随机推荐
How to protect WordPress website from network attack? It is essential to take safety measures
How many of the top ten test tools in 2022 do you master
Top network security prediction: nearly one-third of countries will regulate blackmail software response within three years
New 1688 API access instructions
More interesting Title Dynamic Effect
HCIA configuration instance (ENSP)
[网鼎杯 2020 朱雀组]Nmap
Lombook User Guide
In depth analysis of C language memory alignment
【搜索】—— DFS之剪枝与优化
[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area
数据平台数据接入实践
Comprehensive upgrade, all you can imagine is here -- JD API interface
560 和为 K 的子数组
Slow storage scheme
After understanding the composition of the URL of the website, we use the URL module, querystring module and mime module to improve the static website
For a safer experience, Microsoft announced the first PC with a secure Pluto chip
新生代公链再攻「不可能三角」
科研环境对人的影响是很大的
5G 商用第三年:无人驾驶的“上山”与“下海”