当前位置:网站首页>Sword finger offer 34 A path with a value in a binary tree
Sword finger offer 34 A path with a value in a binary tree
2022-06-21 07:29:00 【South wind knows easy***】
/** * 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 ArrayList<>();
LinkedList<Integer> path=new LinkedList<>();
int target;
public List<List<Integer>> pathSum(TreeNode root, int target) {
this.target=target;
recur(root);
return res;
}
public void recur(TreeNode root){
if(root==null) return;
path.add(root.val);
if(root.left==null&&root.right==null){
int sum=0;
for(int item:path){
sum+=item;
}
if(sum==target) res.add(new ArrayList(path)); Be careful !!!!
}
if(root.left!=null){
recur(root.left);
}
if(root.right!=null){
recur(root.right);
}
path.removeLast();
}
}
边栏推荐
- Mingming has just changed his profession and won five offers as soon as he graduated
- Hisilicon series mass production hardware commissioning record
- 传输层 TCP首部-序号和确认号
- Golang Sync. Use and principle of waitgroup
- . Net 4.5 asynchronous programming pilot (async and await)
- Open up the connection between production and marketing and build a new digital supply chain of agricultural and sideline products
- sql与mysql有哪些区别
- Digital twin smart server: information security monitoring platform
- Kubernetes cluster setup detailed tutorial
- 32单片机——pwm波输出
猜你喜欢

RDKIT | 基于分子指纹的分子相似性

18 statistics and its sampling distribution chi square distribution-t distribution-f distribution

基于Flexsim的供应链建模与仿真课程设计

ETF operation practice record: February 22, 2022

MATLAB 三维图(非常规)

Easyexcel introduction-01

Postman publishing API documentation

如何让mysql不区分大小写

Root cause analysis | inventory of nine scenarios with abnormal status of kubernetes pod

【osg】OSG开发(03)——构建MSVC版的osgQt库
随机推荐
Research Report on market supply and demand and strategy of oil-free scroll compressor industry in China
Minesweeping - C language - Advanced (recursive automatic expansion + chess mark)
模拟手机设备长按事件
Using XAML only to realize the effect of ground glass background panel
Four necessary steps for building a digital factory
18 statistics and its sampling distribution chi square distribution-t distribution-f distribution
[QT] article summarizes the MSVC compilation suite in qtcreator
Can customer managers be relied on online? Is the fund safe
如何利用MES管理系统实现防错和预警
QML控件類型:Drawer
Necessary free artifact for remote assistance todesk remote control software (defense, remote, debugging, office) necessary remote tools
The left column of WordPress implementation shows the article directory
Is the account with low commission safe? Is there a shortage of funds
Pinia advanced: elegant setup (functional) writing + encapsulation into your enterprise project
C # basic knowledge series 8 (const and readonly keywords)
建设数字化工厂的四个必要步骤
Why do smart cities need digital twins?
MySQL MHA
Vite + whistle: a development environment proxy solution once and for all
mysql如何关闭事务