当前位置:网站首页>[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);
}
}
边栏推荐
猜你喜欢

考研大事件!这6件事考研人必须知道!

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

postgresql之page分配管理(一)

HTB-Mirai

The basic knowledge of scripting language Lua summary

SAP ABAP OData 服务如何支持创建(Create)操作试读版
![[LiteratureReview]Optimal and Robust Category-level Perception: Object Pose and Shape Estimation f](/img/bc/f3cea50c157f151a1ca5e540e7f77b.png)
[LiteratureReview]Optimal and Robust Category-level Perception: Object Pose and Shape Estimation f

十九届浙大城院程序设计竞赛 F.Sum of Numerators(数学/找规律)

热心肠:关于肠道菌群和益生菌的10个观点

iPhone难卖,被欧洲反垄断的服务业务也难赚钱了,苹果的日子艰难
随机推荐
灵魂发问:MySQL是如何解决幻读的?
Wovent Bio IPO: Annual revenue of 480 million pension fund is a shareholder
28uA待机8米距离低压保护单片机探头太阳能灯人体PIR定制方案
超全!全国近90所大学考研报录比汇总!
sql中常用到的正则表达
D - Draw Your Cards(模拟)
win10+Qt5.15.2实现低功耗蓝牙控制
Gradle series - Gradle tests, Gradle life cycle, settings.gradle description, Gradle tasks (based on Groovy documentation 4.0.4) day2-3
Pytorch —— 分布式模型训练
Batch replace tables in Word with pictures and save
对标丰田!蔚来又一新品牌披露:产品价格低于20万
【每日一题】1161. 最大层内元素和
JMP Pro 16.0 software installation package download and installation tutorial
leetcode.26 删除有序数组中的重复项(set/直接遍历)
牛客刷SQL--7
六石编程学:问题要面对,办法要技巧,做不好的功能要想办法
HTB-Mirai
四足机器人软件架构现状分析
170页6万字智慧能源管理平台建设方案书
搭建LNMT架构