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. How tomcat works(深入剖析tomcat)servlet容器

    How tomcat works (5)servlet容器阅读笔记 第四章阅读了tomcat默认连接器的实现,当时connector中的使用的容器是自定义的容器,也是非常之简单奥,一个人就干完了所有的 ...

  2. .Net Core AddTransient、AddScoped和AddSingleton的使用

    区别: AddTransient 每次service请求都是获得不同的实例,暂时性模式:暂时性对象始终不同,无论是不是同一个请求(同一个请求里的不同服务)同一个客户端,每次都是创建新的实例 AddSc ...

  3. kali修改配置文件后并保存

    vim是进入配置文件并修改 修改完按Esc进入控制模式,再:w保存 :wq是保存并退出

  4. ModelViewSet基础操作

    1 安装DjangoRestFramework pip install djangorestframework==3.11.1 2.在settings.py中注册 INSTALLED_APPS = [ ...

  5. 《高并发下的.NET》第2季 - 故障公告:高并发下全线崩溃

    大家好,非常抱歉,在昨天下午(12月3日)的访问高峰,园子迎来更高的并发,在这样的高并发下,突发的数据库连接故障造成博客站点全线崩溃,由此给您带来很大的麻烦,请您谅解. 最近,我们一边在忙于AWS合作 ...

  6. 20200428_在centos7.2上挂载ntfs和备份文件到移动硬盘

    [root@localhost ~]# fdisk -l 磁盘 /dev/sda:2000.4 GB, 2000398934016 字节,3907029168 个扇区 - 设备 Boot Start ...

  7. day3(django配置跨域)

    1.跨越原理 1. 首先浏览器安全策略限制js ajax跨域访问服务器 2. 如果服务器返回的头部信息中有当前域: // 允许 http://localhost:8080 这个网站打开的页面中的js访 ...

  8. OpenCV阈值处理函数threshold处理32位彩色图像的案例

    ☞ ░ 前往老猿Python博文目录 ░ 一.概述 openCV图像的阈值处理又称为二值化,之所以称为二值化,是它可以将一幅图转换为感兴趣的部分(前景)和不感兴趣的部分(背景).转换时,通常将某个值( ...

  9. PHP代码审计分段讲解(13)

    代码审计分段讲解之29题,代码如下: <?php require("config.php"); $table = $_GET['table']?$_GET['table']: ...

  10. PHP代码审计分段讲解(12)

    28题 <!DOCTYPE html> <html> <head> <title>Web 350</title> <style typ ...