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. C++/Java小白解Leetcode题,发现了知识盲区……

    一.初见LeetCode 大一时候学习C++,根据课程一直在PTA平台做题目,数据结构和算法的作业题目也是在PTA.后来发现牛客网学习资源也很丰富,孤陋寡闻,前几个月在知道LeetCode这个平台,跟 ...

  2. NTML

     NTLM:         1.客户端向服务器发送一个请求,请求中包含明文的登陆用户名.在服务器中已经存储了登陆用户名和对应的密码hash         2.服务器接收到请求后,NTLMv2协议下 ...

  3. fist-第七天冲刺随笔

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...

  4. PyQt学习随笔:Qt Designer调用帮助时报错找不到assistant.exe文件的解决办法

    老猿是按照<第15.5节 PyQt5安装与配置>步骤安装的PyQt及相关工具,但最近在Qt Designer中点击帮助相关菜单时报错找不到assistant.exe文件,如图: 按照报错信 ...

  5. Python & PyQt学习随笔:PyQt主程序的基本框架

    在完成UI设计将UI通过PyUic转成Py文件后,由于这个生成的文件每次通过PyUic生成时都会被覆盖,因此应用的主程序必须另外单独编写py文件.需要将UI生成的文件import到主程序的py文件中. ...

  6. 手机APP测试(测试点、测试流程、功能测试)

    1.功能测试 1.1 启动 APP安装完成后,是否可以正常打开,稳定运行 APP的速度是可以让人接受,切换是否流畅 网络异常时,应用是否会崩溃:在请求超时的情况下,如果程序逻辑处理的不好,就有可能发生 ...

  7. 一个小时学会用 Go 编写命令行工具

    前言 最近因为项目需要写了一段时间的 Go ,相对于 Java 来说语法简单同时又有着一些 Python 之类的语法糖,让人大呼"真香". 但现阶段相对来说还是 Python 写的 ...

  8. 大白话详解大数据hive知识点,老刘真的很用心(2)

    前言:老刘不敢说写的有多好,但敢保证尽量用大白话把自己复习的内容详细解释出来,拒绝资料上的生搬硬套,做到有自己的了解! 1. hive知识点(2) 第12点:hive分桶表 hive知识点主要偏实践, ...

  9. PhotoSwipe用法

    1.自动识别data-size问题,添加以下代码 gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options) ...

  10. STL—— 容器(vector)元素的删除

    1. clear() 将整个 vector 都删除 使用 vectorname.clear() 可以将整个vector 中的元素全部删除,但是内存不会释放,如下代码: 1 #include <i ...