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系列的更多相关文章

  1. 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 ...

  2. [Leetcode] Combination Sum 系列

    Combination Sum 系列题解 题目来源:https://leetcode.com/problems/combination-sum/description/ Description Giv ...

  3. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. LeetCode Path Sum IV

    原题链接在这里:https://leetcode.com/problems/path-sum-iv/description/ 题目: If the depth of a tree is smaller ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. uni-app 封装接口request请求

    我们知道一个项目中对于前期架构的搭建工作对于后期的制作有多么重要,所以不管做什么项目我们拿到需求后一定要认真的分析一下,要和产品以及后台沟通好,其中尤为重要的一个环节莫过于封装接口请求了.因为前期封装 ...

  2. 基于 RabbitMQ-EasyNetQ 实现.NET与Go的消息调度交互

    基于 RabbitMQ 实现跨语言的消息调度 微服务的盛行,使我们由原来的单机"巨服务"的项目拆分成了不同的业务相对独立的模块,以及与业务不相关的中间件模块.这样我们免不了在公司不 ...

  3. PyQt(Python+Qt)学习随笔:Qt Designer中部件mimimumSize和maximumSize的含义

    1.mimimumSize mimimumSize表示部件能被缩小到的最小尺寸,单位为像素,缩小到该尺寸后不能再进一步缩小了.如果部件在布局管理器中,且布局管理器也设置了最小尺寸,则部件本身的最小尺寸 ...

  4. 【系统设计】WMS系统中 库存、盘点、移库、拆库功能的设计(库内管理)

    最近负责WMS系统 盘点 移库 两个功能模块的功能及数据库设计. 物流仓储系统的搭建,要基于仓库的实际情况,整理内部员工需求,再参考其他WMS系统,经过长时间的讨论和研究,最终转化为产品需求. 这里先 ...

  5. XDown单文件版 下载工具 支持磁力等多种链接方式下载

    原来的程序不带剪辑板探测,不支持迅雷链接等 增加功能后优化制作单文件版本. 下载类型为下图 magnet:?xt=urn:btih:836A228D932EF1C7EA1DD99D5D80B7CB0C ...

  6. 再也不怕 JavaScript 报错了,怎么看怎么处理都在这

    在开发中,有时,我们花了几个小时写的 JS 代码,在游览器调试一看,控制台一堆红,瞬间一万头草泥马奔腾而来.至此,本文主要记录 JS 常见的一些报错类型,以及常见的报错信息,分析其报错原因,并给予处理 ...

  7. LeetCode初级算法之数组:26 删除排序数组中的重复项

    删除排序数组中的重复项 题目地址:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ 给定一个排序数组,你需要在 ...

  8. Scrum 冲刺 第三篇

    Scrum 冲刺 第三篇 每日会议照片 昨天已完成工作 队员 昨日完成任务 黄梓浩 继续完成app项目架构搭建 黄清山 完成部分个人界面模块数据库的接口 邓富荣 完成部分后台首页模块数据库的接口 钟俊 ...

  9. ios移动端 clipboard点击复制失效

    在使用clipboard.min.js插件库实现复制,android下没有问题,ios下无效! 原因:ios默认非点击标签没有点击效果 解决方法:需要给非点击标签加事件,比如在span,div或者p标 ...

  10. HBuilder云端打包+个推

    1.个推上登记应用. 应用名称和应用标识,在HBuilder的云端打包配置中获取. 应用证书:必需要有苹果开发者账号,并且加入了"iOS Developer Program".加入 ...