当前位置:网站首页>LeetCode 练习——113. 路径总和 II
LeetCode 练习——113. 路径总和 II
2022-07-07 07:29:00 【SK_Jaco】
1.题目描述
113. 路径总和 II
给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
输出:[[5,4,11,2],[5,8,4,5]]
示例 2:
输入:root = [1,2,3], targetSum = 5
输出:[]
示例 3:
输入:root = [1,2], targetSum = 0
输出:[]
2.解题思路与代码
2.1 解题思路
这道题求从根节点到叶节点的上和为目标的路径,可以使用前序遍历进行处理。当节点不为叶节点时,将当前节点放入路径列表中,并且目标值减去当前节点值;如果当前节点是叶节点时,就需要判断 target 是否为 0,如果是0,那么就把当前路径放入结果列表中;如果不是0,则返回。在处理完左节点和右节点之后需要将当前节点从路径列表中删除。以题目示例 1 为例进行图解:
首先从根节点开始向下遍历二叉树,将当前节点放入路径列表中,同时 target 减去当前节点值。
当遍历到叶节点时,判断 target 是否为 0,此时不为 0,因此这条路径不满足要求,返回上一节点。
返回同时需要将当前节点从路径列表中移除,然后继续向下到 2 这个节点。此时 target 等于 0,满足要求,将 [5, 4, 11, 2] 这条路径放入结果中。重复上面步骤继续遍历知道结束为止。
2.2 代码
class Solution {
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int target) {
if (root == null) {
return ans;
}
process(root, new ArrayList<>(), target);
return ans;
}
public void process(TreeNode root, List<Integer> tmp, int target) {
if (root == null) {
return;
}
tmp.add(root.val);
target -= root.val;
if (root.left == null && root.right == null && target == 0){
// 如过当前节点为叶节点,并且 target 等于 0,将路径放入结果列表
ans.add(new ArrayList<>(tmp));
}
process(root.left, tmp, target);
process(root.right, tmp, target);
// 处理完左右节点之后将当前节点从路径列表中移除
tmp.remove(tmp.size() - 1);
}
}
2.3 测试结果
通过测试
3.总结
- 使用前序遍历进行解答
- 每个节点在处理完左右节点后需要将当前节点移出路径列表
边栏推荐
- arcgis操作:dwg数据转为shp数据
- 大佬们,有没有遇到过flink cdc读MySQLbinlog丢数据的情况,每次任务重启就有概率丢数
- 反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
- Pit encountered by vs2015 under win7 (successful)
- 使用BigDecimal的坑
- Write it into the SR table in the way of flinksql. It is found that the data to be deleted has not been deleted. Refer to the document https://do
- 2020ccpc Weihai J - Steins; Game (SG function, linear basis)
- Luogu p2482 [sdoi2010] zhuguosha
- Analyze Android event distribution mechanism according to popular interview questions (II) -- event conflict analysis and handling
- Advanced function learning in ES6
猜你喜欢

Delete a record in the table in pl/sql by mistake, and the recovery method

“十二星座女神降临”全新活动推出

内存==c语言1

The request object parses the request body and request header parameters

一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系

Use 3 in data modeling σ Eliminate outliers for data cleaning

Flex flexible layout

ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作

ORM--分组查询,聚合查询,查询集QuerySet对象特性

Basic chapter: take you through notes
随机推荐
Delete a record in the table in pl/sql by mistake, and the recovery method
VS Code指定扩展安装位置
Natapp intranet penetration
高斯消元
Postman interface test I
【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
The Himalaya web version will pop up after each pause. It is recommended to download the client solution
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
Advanced function learning in ES6
Deadlock caused by non clustered index in SQL Server
2020 Zhejiang Provincial Games
EXT2 file system
Bit operation ==c language 2
Use 3 in data modeling σ Eliminate outliers for data cleaning
phpcms实现PC网站接入微信Native支付
MCU与MPU的区别
Three years after graduation
uboot机构简介
ORM model -- creation and query of data records
[original] what is the core of programmer team management?