当前位置:网站首页>Leetcode Brush Questions - Path Sum
Leetcode Brush Questions - Path Sum
2022-08-04 11:13:00 【lonelyMangoo】
概述
记录了112. 路径总和、 113. 路径总和 II、437. 路径总和 III.
It's all done with recursion,里面有一些细节,还是通过递归Analyse ideas.
112. 路径总和
题目:112. 路径总和
There are certain conditions here,It is from the root node to the leaf node.So you only need to see when the leaf node is reached,Whether the target value has been clipped to0.
Analysis directly from the code
//Why there is a return value here recursion can use return value?Still as said before,Because the return value is used.
//Why can the parameters be written into the method body here?Because in the entire method body,What is needed is currenttargetSum,It will not be affected by the front and back.
public boolean hasPathSum(TreeNode root, int targetSum) {
//终止条件
if(root == null) return false;
//函数体,The current size of the node to the leaf node.
targetSum-=root.val;
//找到了对应的值,终止条件
if(root.left ==null && root.right == null){
return targetSum == 0;
}
//The left and right subtrees can be paired with the right one,The return value is used here.
return hasPathSum(root.left,targetSum) || hasPathSum(root.right,targetSum);
}

113. 路径总和 II
题目:113. 路径总和 II
The difference between this question and the previous one is that all paths need to be found.
那么就需要用一个list来保存.Sounds about the same as before,But with a lot of caveats,Details in the code.
//Why put it outside the recursive method body and not inside it,Because every time it is required to be up to date
private List<List<Integer>> res = new ArrayList<>();
List<Integer> list =new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
find(root,targetSum);
return res;
}
//为什么没有返回值,Because it doesn't need to be used,What is needed here is to recursively find all the results,The return value does not need to be processed.
private void find(TreeNode root, int targetSum ){
if(root == null){
return;
}
//Count the current node into it.
list.add(root.val);
targetSum-=root.val;
if(root.left ==null && root.right == null){
if( targetSum == 0){
//这里一定要new !!!
//Otherwise a reference is stored
res.add(new ArrayList(list));
}
}
find(root.left,targetSum);
find(root.right,targetSum);
//The current node has been processed,just remove it.
list.remove(list.size()-1);
}

437. 路径总和 III
题目:437. 路径总和 III
This question removes the conditions for the root and leaf nodes,Continuing the idea above,Just make each node can be used as a leaf node and a root node.But there was a problem in the middle,There will be repetitions.
比如:
如果目标节点是4的话,1-2 会到4
但是从1The branch that is branched out is directly from2The beginning will also arrive4,4was counted twice.
Go directly to the code for analysis
int sum = 0;
public int pathSum(TreeNode root, int targetSum) {
judge(root,targetSum,true);
return sum;
}
//Two parameters are placed on the method body,Because it is a local,并不是全局的.
public void judge(TreeNode root, long count, boolean isStartNode) {
if (root == null) {
return;
}
if (count == root.val) {
sum++;
}
//Here is the head node,如果true才能继续
//Prevent multiple hits from happening
//If so recursive,At every point there is an opportunity.
if (isStartNode ){
judge(root.left, count, true);
judge(root.right, count, true);
}
//When the head node is determined,往下搜索.
//The following nodes are not head nodes,There is no need to follow the above judgment logic.isStartNode设置为false
if (root.left != null) {
judge(root.left, count-root.val, false);
}
if (root.right != null) {
judge(root.right, count-root.val, false);
}
}

边栏推荐
猜你喜欢

The use of DDR3 (Naive) in Xilinx VIVADO (2) Read and write design

Maple 2022软件安装包下载及安装教程

【Inspirational】The importance of review
![[Hongke case] Assembling furniture based on 3D camera](/img/00/bd04f9445add2571ad9cf276e81cb1.png)
[Hongke case] Assembling furniture based on 3D camera

美摄问答室|美映 VS 美摄云剪辑

A topic of map

CVPR 2022 | 从人体网格预测骨架,是真正的生理学骨架!

【LeetCode】700.二叉搜索树

The use of DDR3 (Naive) in Xilinx VIVADO (3) simulation test

图文手把手教程--ESP32 MQTT对接EMQX本地服务器(VSCODE+ESP-IDF)
随机推荐
Xilinx VIVADO 中 DDR3(Naive)的使用(1)创建 IP 核
Redis查询缓存
Zikko上市同时搭载HDMI2.1和2.5GbE新款雷电4扩展坞
JUC(1)线程和进程、并发和并行、线程的状态、lock锁、生产者和消费者问题
*iframe*
解析treeSet集合进行自定义类的排序
What is the terminal privilege management
MySql数据库入门的基本操作
数字知识库及考学一体化平台
【Idea series】idea configuration
秒云成功入选《2022爱分析 · 银行数字化厂商全景报告》,智能运维能力获认可
Advanced transcriptome analysis and R data visualization hot registration (2022.10)
【LeetCode】653. 两数之和 IV - 输入 BST
Graphical Hands-on Tutorial--ESP32 One-Key Network Configuration (Smartconfig, Airkiss)
章节小测一
网管交换机与非网管交换机如何选择?
复盘:经典的HR面试问题,这些问题可以挖掘你个人的素质,看看你是否合适合我们部门
datax oracle to oracle incremental synchronization
8月活动|51CTO十七周年庆,发博文得茶具/笔记本/T恤等礼品!
Super Learning Method