当前位置:网站首页>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);
}
}

边栏推荐
- 【飞控开发高级教程7】疯壳·开源编队无人机-编队飞行
- Leetcode——利用先序遍历特性完成114. 二叉树展开为链表
- JUC(1)线程和进程、并发和并行、线程的状态、lock锁、生产者和消费者问题
- C language * Xiaobai's adventure
- Leetcode brush questions - binary search tree related topics (98. Verify binary search tree, 235. The nearest common ancestor of binary search tree, 1038. From binary search tree to bigger sum tree, 5
- Leetcode刷题——构造二叉树(105. 从前序与中序遍历序列构造二叉树、106. 从中序与后序遍历序列构造二叉树)
- Leetcode刷题——二叉搜索树相关题目(98. 验证二叉搜索树、235. 二叉搜索树的最近公共祖先、1038. 从二叉搜索树到更大和树、538. 把二叉搜索树转换为累加树)
- Advanced transcriptome analysis and R data visualization hot registration (2022.10)
- AWS Lambda相关概念与实现思路
- Learn to use the basic interface of set and map
猜你喜欢

*iframe*

Camunda overall architecture and related concepts

mae,mse,rmse分别利用sklearn和numpy实现

ECCV 2022 | 清华&腾讯AI Lab提出REALY: 重新思考3D人脸重建的评估方法

Xilinx VIVADO 中 DDR3(Naive)的使用(1)创建 IP 核

Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)

Events in August | 51CTO's 17th Anniversary Celebration, post a blog post to get gifts such as tea sets/notebooks/T-shirts!

123

The use of DDR3 (Naive) in Xilinx VIVADO (1) to create an IP core

强烈推荐一款优秀且通用的后台管理系统
随机推荐
Digital management insight into retail and e-commerce operations - retail password
Mysql数据类型
MySQL不提供数组,只能做成表吗?
Leetcode刷题——构造二叉树(105. 从前序与中序遍历序列构造二叉树、106. 从中序与后序遍历序列构造二叉树)
Advanced transcriptome analysis and R data visualization hot registration (2022.10)
zabbix部署
使用json-server快速搭建本地数据接口
C#/VB.NET:在 Word 中设置文本对齐方式
RL78开发环境
Apache Calcite 框架原理入门和生产应用
使用.NET简单实现一个Redis的高性能克隆版(二)
第二批养老理财试点产品发行 一小时销售20亿元
The use of DDR3 (Naive) in Xilinx VIVADO (3) simulation test
Jenkins使用手册(1) —— 软件安装
上帝空间——全球首个基于Web3.0的艺术协议创意平台,拓宽多元艺术融合边界
【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
网管交换机与非网管交换机如何选择?
win8和win10下,visual studio 2008 调试出现无响应的卡死问题解决
shell变量
Redis查询缓存