当前位置:网站首页>Leetcode exercise - 113 Path sum II
Leetcode exercise - 113 Path sum II
2022-07-07 10:11:00 【SK_ Jaco】
1. Title Description
113. The sum of the paths II
Give you the root node of the binary tree root And an integer target and targetSum , Find out all From the root node to the leaf node The path sum is equal to the given target sum .
Leaf node A node without children .
Example 1:
Input :root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output :[[5,4,11,2],[5,8,4,5]]
Example 2:
Input :root = [1,2,3], targetSum = 5
Output :[]
Example 3:
Input :root = [1,2], targetSum = 0
Output :[]
2. Problem solving ideas and codes
2.1 Their thinking
This problem is to find the path from the root node to the upper sum of the leaf node as the target , You can use preorder traversal for processing . When the node is not a leaf node , Put the current node into the path list , And the target value minus the current node value ; If the current node is a leaf node , You need to judge target Is it 0, If it is 0, Then put the current path into the result list ; If not 0, Then return to . After processing the left and right nodes, you need to delete the current node from the path list . Take the topic as an example 1 As an example :
First, traverse the binary tree downward from the root node , Put the current node into the path list , meanwhile target Subtract the current node value .
When traversing the leaf node , Judge target Is it 0, Not at this time 0, Therefore, this path does not meet the requirements , Return to the previous node .
When returning, you need to remove the current node from the path list , Then continue down to 2 This node . here target be equal to 0, Meet the requirements , take [5, 4, 11, 2] This path is put into the result . Repeat the above steps and continue to traverse until the end .
2.2 Code
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){
// If the current node is a leaf node , also target be equal to 0, Put the path into the result list
ans.add(new ArrayList<>(tmp));
}
process(root.left, tmp, target);
process(root.right, tmp, target);
// After processing the left and right nodes, remove the current node from the path list
tmp.remove(tmp.size() - 1);
}
}
2.3 test result
Pass the test 
3. summary
- Use preorder traversal to solve
- Each node needs to move the current node out of the path list after processing the left and right nodes
边栏推荐
- Can I open a stock trading account online? Is it safe
- 反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
- Win10安装VS2015
- Deadlock caused by non clustered index in SQL Server
- Huffman encoded compressed file
- LLVM之父Chris Lattner:為什麼我們要重建AI基礎設施軟件
- 柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
- Finally, there is no need to change a line of code! Shardingsphere native driver comes out
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- Do you have a boss to help look at this error report and what troubleshooting ideas are there? Oracle CDC 2.2.1 flick 1.14.4
猜你喜欢

Bean 作⽤域和⽣命周期

A wave of open source notebooks is coming

中国首款电音音频类“山野电音”数藏发售来了!

Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT

Deep understanding of UDP, TCP

arcgis操作:dwg数据转为shp数据

Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat

Introduction to energy Router: Architecture and functions for energy Internet

The landing practice of ByteDance kitex in SEMA e-commerce scene

Arcgis操作: 批量修改属性表
随机推荐
每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
There is a problem using Chinese characters in SQL. Who has encountered it? Such as value & lt; & gt;` None`
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
Mongodb creates an implicit database as an exercise
ORM--查询类型,关联查询
Can I open a stock trading account online? Is it safe
能源路由器入门必读:面向能源互联网的架构和功能
一大波开源小抄来袭
ORM -- query type, association query
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
中国首款电音音频类“山野电音”数藏发售来了!
Postman interface test VI
Internship log - day07
基于gis三维可视化技术的智慧城市建设
Using keras in tensorflow to build convolutional neural network
Methods of adding centerlines and centerlines in SolidWorks drawings
Bit operation ==c language 2
反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
CDZSC_ 2022 winter vacation personal training match level 21 (1)