[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 ...
随机推荐
- 【Usaco 2009 Gold】JZOJ2020年9月19日提高B组T4 过路费
[Usaco 2009 Gold]JZOJ2020年9月19日提高B组T4 过路费 题目 Description 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生财之 ...
- vue回调接口
1.微博回调接口 1.1oauth/urls.py 中添加路由 urlpatterns = [ path('weibo/callback/', views.OauthWeiboCallback.as_ ...
- PyQt(Python+Qt)学习随笔:QListWidget获取指定项对应行的row方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListWidget的row方法通过项作为参数,获取到对应项所在行的行号,语法如下: int ro ...
- PyQt(Python+Qt)学习随笔:QListWidgetItem的重要方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListWidgetItem类为QListWidget类提供构成QListWidget列表部件的项 ...
- PyQt及PyCharm学习中遇到的问题
在PyQt及PyCharm学习过程中,老猿遇到了如下问题: 问题: 刚安装的PyCharm执行代码报"ModuleNotFoundError: No module named XXXX&qu ...
- PyQt学习随笔:QStandardItemModel使用注意事项
老猿Python博文目录 老猿Python博客地址 在使用QStandardItemModel或其派生类作为view对象的数据存储时,有如下几点需要注意: 1.如果是多行多列的数据存储,对应视图如果没 ...
- JAVA课堂随机出题
一.设计思路 1.利用随机数来确定两个数字. 2.生成0-4的随机数,分别代表 加 减 乘 除. 3.输入的题数利用for循环来出题,每行输出几道题便在循环中加入if语句,当前出题数与每行输出题数求余 ...
- 6、Sping Cloud Feign
1.Spring Cloud Feign简介 (1).Fegin简介 官方文档:http://projects.spring.io/spring-cloud/spring-cloud.html#spr ...
- Java 线程安全问题的本质
原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 目录: 线程安全问题的本质 理解CPU JVM虚拟机类比于操作系统 重排序 汇总 一些解释 ...
- jxl导出excel小demo
1.首先在pom文件加入jar包 <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <art ...