[LeetCode]Path Sum系列
1.二叉树路径求指定和,需要注意的是由于有负数,所以即使发现大于目标值也不能返回false,而且返回true的条件有两个,到叶节点且等于sum,缺一不可
public boolean hasPathSum(TreeNode root, int sum) {
if (root==null) return false;
if (root.val==sum&&root.left==null&&root.right==null) return true;
else return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum- root.val);
}
2.跟第一题的不同是要把所有路径返回,做法一样,把所有路径都遍历一遍,每次递归返回后都要回溯,把符合要求的路径记录
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> tp=new ArrayList<List<Integer>>();
List<Integer> cu=new ArrayList<Integer>();
int total=0;
dfs(root, tp, cu,total,sum);
return tp;
}
public void dfs(TreeNode root,List<List<Integer>> tp, List<Integer> cu,int total,int sum){
if(root==null)
return;
cu.add(root.val);
total=total+root.val;
if(root.left==null&&root.right==null&&total==sum){
tp.add(new ArrayList(cu));
return;
}
if (root.left!=null){
dfs(root.left,tp, cu,total,sum);
cu.remove(cu.size()-1);
}
if (root.right!=null){
dfs(root.right,tp, cu,total,sum);
cu.remove(cu.size()-1);
}
}
3.不要求开头和结尾是根节点和叶节点,但是顺序必须自上而下,做法是递归的把所有节点都当做根节点判断一遍,每次判断时不同的地方是不用判断到没到叶节点,而且=target后不返回,只是结果+1,继续向下判断。
public int pathSum(TreeNode root, int sum) {
int cu=0;
if (root == null)
return 0;
return dfs(root,sum,cu)+pathSum(root.left,sum)+pathSum(root.right,sum);
// 开始的点不一定是根节点,采取的方法是返回根节点函数+左子节点函数+右子节点函数,这样递归后就可以实现所有节点都可以作为开始
}
public int dfs(TreeNode root,int sum,int cu){
int re=0;
if (root == null)
return re;
cu+=root.val;
if(cu == sum)
re++;
re+=dfs(root.left,sum,cu);
re+=dfs(root.right,sum,cu);
return re;
}
[LeetCode]Path Sum系列的更多相关文章
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- [Leetcode] Combination Sum 系列
Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- LeetCode Path Sum IV
原题链接在这里:https://leetcode.com/problems/path-sum-iv/description/ 题目: If the depth of a tree is smaller ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
随机推荐
- 【常见踩坑】】USB调试安装失败(Installation failed with message INSTALL_CANCELED_BY_USER)
[参考]http://www.cnblogs.com/liushilin/p/6553918.html 问题:在USB安装调试(小米手机),出现如下错误 解决:1.小米手机解决办法见参考.登录小米账号 ...
- web服务器专题:tomcat基础及模块
Web服务器专题:Tomcat(一)基础架构 针对java系的经典服务器,打算系统的整理一下Tomcat的机制和一些原理,以此记录. 插一则题外话,关于tomat这个名字的由来:Tomcat 名称的由 ...
- jquery on 动态生成绑定事件
$(document).on("mouseenter", ".v6-div-kind-ok", function () { alert();});
- socket阻塞与非阻塞,同步与异步,select,pool,epool
概念理解 一.与I/O相关的五个重要概念 1. 第一个概念:用户空间与内核空间 1. 现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方) 2. ...
- moviepy音视频剪辑:使用autoTrack、manual_tracking+headblur实现半自动追踪人脸打马赛克
一.引言 在<moviepy1.03音视频剪辑:使用manual_tracking和headblur实现追踪人脸打马赛克>介绍了使用手动跟踪跟踪人脸移动轨迹和使用headblur对人脸进行 ...
- PyQt(Python+Qt)学习随笔:QTabWidget选项卡部件移除选项卡的removeTab和clear方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 要从一个QTabWidget中去除选项卡,可用使用removeTab和clear方法. 1.移除选项 ...
- 团队作业4-Day3
团队作业4-Day3 项目git地址 1. 站立式会议 2. 项目燃尽图 3. 适当的项目截图 今日暂无较大代码更新 4. 代码/文档签入记录(部分) 5. 每人每日总结 吴梓华:今天未进行开发,学习 ...
- 【AtCoder AGC023F】01 on Tree(贪心)
Description 给定一颗 \(n\) 个结点的树,每个点有一个点权 \(v\).点权只可能为 \(0\) 或 \(1\). 现有一个空数列,每次可以向数列尾部添加一个点 \(i\) 的点权 \ ...
- POWER BI 基于 ODBC 数据源的配置刷新-以Amazon Redshift为例
POWER BI 基于 ODBC 数据源的配置刷新-以Amazon Redshift为例 Powerbi 有多种数据源连接,可以使用它们连接到不同数据源. 如果在 Power BI Desktop 的 ...
- 纯css 实现文本换行
业务场景:dialog中嵌套的表单,实现信息展示,由于存储路径过长并且在一行显示,导致多出的文字出现在弹出框外面了,页面极丑,所以需要将存储路径实现自动换行. 技术点:<p style=&quo ...