当前位置:网站首页>[Binary Tree] Path Sum II
[Binary Tree] Path Sum II
2022-08-01 14:04:00 【Don't write blog is not great】
路径总和II
这道题目10分钟就可以完成了,I've gotten a lot out of practice lately,加油
路径总和,Divide into left and right subtrees,要注意的是JavaThe path needs to be additionally saved here,Otherwise, save directlypath都是null
/** * 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 {
private List<List<Integer>> ret = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
if(root == null) return ret;
List<Integer> path = new ArrayList<>();
searchPath(root, targetSum, path);
return ret;
}
private void searchPath(TreeNode root, int targetSum, List<Integer> path){
if(root == null) return;
if(root.left == null && root.right == null){
if(root.val == targetSum){
List<Integer> ans = new ArrayList<>();
for(int i = 0; i < path.size(); i++){
ans.add(path.get(i));
}
ans.add(root.val);
ret.add(ans);
}
}
path.add(root.val);
searchPath(root.left, targetSum-root.val, path);
searchPath(root.right, targetSum-root.val, path);
path.remove(path.size()-1);
}
}
边栏推荐
- 微服务原生案例搭建
- 批量替换Word中的表格为图片并保存
- Batch replace tables in Word with pictures and save
- [机缘参悟-57]:《素书》-4-修身养志[本德宗道章第四]
- 消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?
- 2022图片在线加水印源码
- 安培龙IPO过会:年营收5亿 同创伟业与中移创新是股东
- kubernetes之DaemonSet以及滚动更新
- Performance Optimization - Rendering Optimization Notes
- What Can Service Mesh Learn from SDN?
猜你喜欢
消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?
PAT1166 Summit(25)
PAT 1163 Dijkstra Sequence(30)
重磅!国内首个开放式在线绘图平台Figdraw突破10万用户!发布《奖学金激励计划》:最高5000元!...
Based on 10 years of experience in stability assurance, what are the three key questions to be answered in failure recovery?|TakinTalks big coffee sharing
考研大事件!这6件事考研人必须知道!
postgresql之page分配管理(一)
gpio analog serial communication
【每日一题】1331. 数组序号转换
A Beginner's Guide to Performance Testing
随机推荐
HTB-Shocker
PAT 1167 Cartesian Tree(30)
微服务原生案例搭建
Multithreading Case - Timer
【每日一题】952. 按公因数计算最大组件大小
fh511小风扇主控芯片 便携式小风扇专用8脚IC 三档小风扇升压芯片sop8
Pytorch —— 分布式模型训练
Performance Optimization - Resource Optimization Notes
DDL和DML的含义与区别「建议收藏」
Qt实战案例(55)——利用QDir删除选定文件目录下的空文件夹
DaemonSet of kubernetes and rolling update
拥抱NFV,Istio 1.1 将支持多网络平面
全球都热炸了,谷歌服务器已经崩掉了
Performance Optimization - Rendering Optimization Notes
CCS软件安装教程(超级详细)「建议收藏」
沃文特生物IPO过会:年营收4.8亿 养老基金是股东
Service Mesher Meetup 成都站:Service Mesh是下一代SDN吗?
gpio analog serial communication
E - Red and Blue Graph(组合数学)
牛客刷SQL--4