当前位置:网站首页>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.总结
- 使用前序遍历进行解答
- 每个节点在处理完左右节点后需要将当前节点移出路径列表
边栏推荐
- 官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
- ES6中的函数进阶学习
- Enterprise practice | construction of banking operation and maintenance index system under complex business relations
- Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
- C socke server, client, UDP
- 企业实战|复杂业务关系下的银行业运维指标体系建设
- conda离线创建虚拟环境
- 【ORM框架】
- Advanced function learning in ES6
- Huffman encoded compressed file
猜你喜欢

ORM -- logical relation and & or; Sort operation, update record operation, delete record operation

Google Colab装载Google Drive(Google Colab中使用Google Drive)

Google colab loads Google drive (Google drive is used in Google colab)

ISP、IAP、ICP、JTAG、SWD的编程特点

The Himalaya web version will pop up after each pause. It is recommended to download the client solution

Applet popup half angle mask layer

request对象对请求体,请求头参数的解析

反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释

Pit using BigDecimal

The request object parses the request body and request header parameters
随机推荐
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
Flex flexible layout
ORM--查询类型,关联查询
位操作==c语言2
虚数j的物理意义
单片机(MCU)最强科普(万字总结,值得收藏)
C socke server, client, UDP
Huffman encoded compressed file
Applet sliding, clicking and switching simple UI
ORM -- query type, association query
Parameter sniffing (2/2)
Apprentissage avancé des fonctions en es6
能源路由器入门必读:面向能源互联网的架构和功能
内存==c语言1
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)
Introduction to energy Router: Architecture and functions for energy Internet
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
Es classes and objects, prototypes
Garbage disposal method based on the separation of smart city and storage and living digital home mode