当前位置:网站首页>437. 路径总和 III
437. 路径总和 III
2022-07-21 05:15:00 【安吉_lh1029】
1、题目描述
2、题目分析
通过题目可以得到下来3个条件:
1、求路径数目:一个二叉树的根节点 root
,和一个整数 targetSum
,求该二叉树里节点值之和等于 targetSum
的 路径 的数目。
2、路径 不需要从根节点开始,也不需要在叶子节点结束
3、要求 路径方向必须是向下的(只能从父节点到子节点)
具体代码实现如下:
class Solution {
int sumPath = 0;
public int pathSum(TreeNode root, long targetSum) {
if(root == null) return 0;
sumValue(root,0,targetSum);
//左右子树中任何节点都有成为出发点的可能,因此分别递归
pathSum(root.left,targetSum);
pathSum(root.right,targetSum);
return sumPath;
}
private void sumValue(TreeNode root, long sum, long targetSum){
if(root == null) return ;
sum += root.val;
if(sum == targetSum){
sumPath++;
}
//左右子树分别遍历
sumValue(root.left, sum, targetSum);
sumValue(root.right, sum, targetSum);
}
}
复杂度分析
时间复杂度:O(N^2),其中 N为该二叉树节点的个数。对于每一个节点,求以该节点为起点的路径数目时,则需要遍历以该节点为根节点的子树的所有节点,因此求该路径所花费的最大时间为 O(N),我们会对每个节点都求一次以该节点为起点的路径数目,因此时间复杂度为O(N^2)。
空间复杂度:O(N),考虑到递归需要在栈上开辟空间。
代码中需要注意的点:
1、 //左右子树中任何节点都有成为出发点的可能,因此分别递归
pathSum(root.left,targetSum);
pathSum(root.right,targetSum);
2、在编译时,倒数第二个case编译不过[1000000000,1000000000,null,294967296,null,1000000000,null,1000000000,null,1000000000] 0
原因是数字范围超过int的范围,因此编译出错。
修改完善条件,如上面代码所示,把【目标和】和中间步骤的【累积加】全部由int变成long型。 既 long sum, long targetSum。
边栏推荐
- Summary of actual process and steps of interface test
- 通用流程编排引擎介绍
- 近10年数据仓库演进之路,以及数据库学习建议
- Datart data visualization works are open source | chart plug-in works are all open source, which can be extracted through Baidu cloud download link
- 深入剖析多重背包问题(下篇)
- 西瓜书第二章笔记-评估方法
- Huawei liteos memory management source code and architecture analysis
- 并发开篇——带你从0到1建立并发知识体系的基石
- JMeter - from getting started to mastering - environment construction (detailed tutorial)
- The top three suddenly changed? Unveil the latest ranking of programming languages in July
猜你喜欢
流批一体?实时数据处理场景化应用实例~
Typescript learning notes
Why not use scheduled tasks to close orders?
In depth analysis of LinkedList source code
01-自动化测试基础--(selenium八股文部分+环境配置+八大定位)
线性回归大结局(岭(Ridge)、 Lasso回归原理、公式推导),你想要的这里都有
Qt5 GUI
VS2019+OpenCV安装与配置教程
04 unittest unit test framework
数据可视化应用安装部署 | 使用 datart 安装包和源码部署的常见问题教程
随机推荐
西瓜书第三章-线性模型
火爆社区的开源数据可视化工具 datart 新用户体验教程
Using two templates (recursive method and iterative method) to complete four traversal ways of binary tree
轻松自制PASCAL VOC数据集
Interviewer: have you made a judgment on the number of rows affected by your update?
Using UUID as MySQL primary key, my boss broke up
流批一体?实时数据处理场景化应用实例~
Blockbuster: the domestic ide was released and developed by Alibaba. It is completely open source (high performance + high customization)
深入剖析多重背包问题(上篇)
面试题:聚簇索引和非聚簇索引有什么区别?
MSTest 无法退出
深入剖析多重背包问题(下篇)
三类基于贪心思想的区间覆盖问题
04 unittest unit test framework
01-自动化测试基础--(selenium八股文部分+环境配置+八大定位)
In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
Mstest cannot exit
Do you think sub database and sub table are really suitable for your system? Talk about how to select sub databases, sub tables and newsql
robotframework 常用快捷键
Scientific evidence: does NMN have an effect on cholecystitis, and how about the clinical effect of NMN